The numeric root that describes a positive integer is obtained by summing the number of integers. If the result value is a number, then the number is the number root. If the result value contains two or more digits, add the numbers and repeat the process. You can continue as long as you need to get a digit. For example, consider the addition of positive integers 24.2 and 4 to produce a value of 6. Since 6 is a single number, 6 is the number root of 24. Now consider the addition of positive integers 39.3 and 9. Because 12 is not a single number, you must repeat the process. 1 and 2 Add to get 3,3 is a single number, which is 39 of the number root. The input input file will contain a list of positive integers, one per line. The end of the input is indicated by an integer value of 0. Output for each integer in the input, output its number root on a separate line in the output. Sample input 0 Sample output 63
problem
Think about how to split a value, each time% 10, get the last add, then/10 minus the bottom, repeat.
It is convenient to use the string to store the values of each row, depending on the code implementation:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intFenjie (intnum) { intsum =0; while(num>=Ten) {sum+ = num%Ten; Num/=Ten; } Sum+=num; returnsum;} intMain () {Charsum[1010]; while(Gets (sum) &&sum[0]!='0') { inti,x=0; for(i=0; I<strlen (sum); i++) {x+ = Sum[i]-'0'; } while(x>=Ten) {x=Fenjie (x); } printf ("%d\n", x); } return 0;}
View Code
(HDU) 1013--digital Roots (digital root)