杭电1013

来源:互联网 发布:java不关闭流 编辑:程序博客网 时间:2024/06/07 16:29
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
 
 
 
开始一看,认为很简单,把各个位上的元素相加便可以得到答案,可是这是一个大数,不能用简单的排序,用string找不到位数,后面百度,直接求对于9的余数便可,有规律啊!!!所以以后不能急着做题,先思考。。。
<span style="color:#000099;">#include <iostream>#include<string.h>using namespace std;int main(){ int b=0,a; char h[10001]; while(cin>>h) {  if(strcmp(h,"0")==0)break;  for(a=0;a<strlen(h);a++)   b+=h[a]-'0';//对int下的书求和   while(b>9)    b%=9;   if(b==0)    cout<<9<<endl;   else    cout<<b<<endl;   b=0; } return 0;}</span>


 
0 0
原创粉丝点击