Digital Roots

来源:互联网 发布:淘宝美白面膜排行榜 编辑:程序博客网 时间:2024/04/19 01:32
Description

The digital root of a positive integer is found bysumming the digits of the integer. If the resulting value is asingle digit then that digit is the digital root. If the resultingvalue contains two o r more digits, those digits are summed and theprocess is repeated. This is continued as long as necessary toobtain a single digit.

For example, consider the positive integer 24. Adding the 2 andthe 4 yields a value of 6. Since 6 is a single digit, 6 is thedigital root of 24. Now consider the positive integer 39. Addingthe 3 and the 9 yields 12. Since 12 is not a single digit, theprocess must be repeated. Adding the 1 and the 2 yeilds 3, a singledigit and also the digital root of 39.

Input

The input file will contain a list of positive integers, one perline. The end of the input will be indicated by an integer value ofzero.

Output

For each integer in the input, output its digital root on aseparate line of the output.

Sample Input

24 39 0 

Sample Output

63

 

#include<iostream>
#include<cstring>
using namespace std;
int f(int n)
   intr;
   if(n<=9) return n;
 else
 
  r=0;
   while(n>0)
    {
     r+=n;
   n/=10;
    }
 
 }
 return f(r);
  
}
int main(){
char s[1024];
int n, i;
while (cin>>s){
   if (s[0]=='0'&& s[1]=='\0'){ break; }
   n = 0;
   for (i=strlen(s)-1;i>=0; --i){
     n += s[i]-'0';
   }
 cout<<f(n)<<endl;
}
return 0;
}

0 0
原创粉丝点击