poj 1519 Digital Roots

it2024-03-23  17

Digital Roots

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 22313 Accepted: 7329

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 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

24 39 0

Sample Output

6 3

 

1: #include<iostream> 2: using namespace std; 3: int count(int n) 4: { 5: int res=0; 6: while(n>0) 7: { 8: res+=n%10; 9: n=n/10; 10: } 11: return res; 12: } 13: int main() 14: { 15: int sum; 16: char n[2000]; 17: int i; 18: while(1) 19: { 20: cin>>n; 21: if(strcmp(n,"0")==0) 22: break; 23: sum=0; 24: for(i=0;i<strlen(n);i++) 25: sum+=n[i]-'0'; 26: while(sum>=10) 27: { 28: sum=count(sum); 29: } 30: cout<<sum<<endl; 31: } 32: return 0; 33: } 34:

转载于:https://www.cnblogs.com/w0w0/archive/2011/11/21/2257595.html

相关资源:数据结构—成绩单生成器
最新回复(0)