알고리즘/codefights

41>digitDegree

Diademata 2017. 8. 8. 01:24
반응형

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.


code>>


int digitDegree(int n) {

if (n < 10) return 0;

int sum = 0;

int count = 1;

while (n > 0)

{

int tmp = n % 10;

n /= 10;

sum += tmp;

}

count += digitDegree(sum);

return count;

}



반응형

'알고리즘 > codefights' 카테고리의 다른 글

43>isBeautifulString  (0) 2017.08.11
42>bishopAndPawn  (0) 2017.08.10
40>longestDigitsPrefix  (0) 2017.08.08
39>knapsackLight  (0) 2017.08.08
38>growingPlant  (0) 2017.08.07