Digital roots
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:23650 |
|
Accepted:7843 |
Description
The digital root of a positive integer is found by summing the digits of the integer. if the resulting value is a single digit then that digit is the digital root. if the resulting value contains two or more digits, those digits
Are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.
For example, consider the positive integer 24. adding the 2 and the 4 yields a value of 6. since 6 is a single digit, 6 is the digital root of 24. now consider the positive integer 39. adding the 3 and the 9 yields 12. since 12 is not a single digit, the process
Must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
Input
The input file will contain in a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.
Output
For each integer in the input, output its digital root on a separate line of the output.
Sample Input
24390
Sample output
63
I thought that this question could not be solved, but I learned it only after reading the answers on the Internet. The original test site for this question was on a large number, and it was possible to enter a thousand digits ......
To learn more, you must carefully consider the details of the problem.
# Include <iostream> using namespace STD; int root (int K) {If (k <10) return K; int TMP = 0; while (k) {TMP + = K % 10; k = K/10;} If (TMP> = 10) return root (TMP); else return TMP;} int main () {unsigned int N; char input [2000]; // This topic mainly deals with the processing of large numbers, which may contain thousands of digits. Therefore, the input should be treated as a string while (CIN> input, strcmp (input, "0") // note character operation 1. {n = 0; For (INT I = 0; I <strlen (input); I ++) N + = input [I]-'0 '; // note character operation 2.if( n = 0) break; cout <root (n) <Endl ;}}