Problem DescriptionThe Digital root of a positive integer is found by summing the digits of the integer. If The resulting value is a, digit then, digit is the digital root. If The resulting value contains or more digits, those digits was summed and the process is repeated. This was continued as long as necessary to obtain a and 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 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.
Inputthe input file would contain a list of positive integers, one per line. The end of the input would be indicated by an integer value of zero.
Outputfor each integer in the input and output its digital root on a separate line of the output.
Sample Input
24390
Sample Output
63
In the final analysis, it is important to note that it is possible to give a large number of numbers, so you cannot use int or __int64 to store the string.
#include <stdio.h> #include <string.h> #include <iostream>using namespace Std;int main () { char a[ 1000],B[1000]; int i,j,k,l,ans,t; while (scanf ("%s", a)!=eof) { if (a[0]== ' 0 ') is break ; L=strlen (a); strcpy (b,a); while (1) { ans=0; for (i=0;i<l;i++) { ans+=b[i]-' 0 '; } if (ans>9) {for (i=0;ans!=0;i++) { b[i]= (ans%10) + ' 0 '; ANS=ANS/10; } b[i]= ' + '; } else break; L=strlen (b); } printf ("%d\n", ans);} }
Hdoj (1013)--digital Roots (String simulation)