Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24050 | Accepted: 7970 |
Description
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
Output
Sample Input
24 39 0
Sample Output
6 3
Source
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
cin>>str;
while(str!="0"){
int i = str.size();
int j(0);
for(int k=0;k<i;k++){
j = (j*10 + (str[k]-'0'))%9;
}
if(j==0){
j+=9;
}
cout<<j<<endl;
cin>>str;
}
return 0;
}