hdu 1013(余九法取余)

来源:互联网 发布:java图相关算法 编辑:程序博客网 时间:2024/06/08 15:09

Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 75577    Accepted Submission(s): 23560


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

Sample Output
6
题意
1,把这个数每一位的数字都加起来,得到一个数
2,如果得到的这个数不是一个一位数,就重复 1 操作,否则操作 3
3,得到的这个是就是原先给出的数的数根
余九法求数根:直接把这个数的每个位上的数字都加起来,对 9 取余,如果结果为 0 ,那么结果是 9,否则就是求得的数值!
#include<iostream>#include<string>#include<string.h>#include<sstream>#include<queue>using namespace std;int main(){string s;while(cin>>s,s!="0"){int sum=0;for(int i=0;i<s.size();i++){sum+=s[i]-'0';}if(sum%9==0)cout<<9<<endl;elsecout<<sum%9<<endl;}}


 

0 0
原创粉丝点击