Medium

Let's define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.

Given an integer, find its digit degree.

Example

  • For n = 5, the output should be
    digitDegree(n) = 0;
  • For n = 100, the output should be
    digitDegree(n) = 1.
    1 + 0 + 0 = 1.
  • For n = 91, the output should be
    digitDegree(n) = 2.
    9 + 1 = 10 -> 1 + 0 = 1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    5 ≤ n ≤ 10^9.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int digitDegree(int n)
{
	int count=0;

	while(n%10!=n)
	{
		int N=0;

		while(n>0)
		{
			N+=n%10;
			n/=10;
		}
		n=N;

		count++;
	}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> isBeautifulString  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05
<Codesignal> Knapsack Light  (0) 2020.04.05
<Codesignal> growingPlant  (0) 2020.04.05

+ Recent posts