HDU 1013 Digital Roots
2014-03-24 18:08
344 查看
Digital Roots
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each integer in the input, output its digital root on a separate line of the output.
[align=left]Sample Input[/align]
24
39
0
[align=left]Sample Output[/align]
6
3
题目大意:求各个位的数字相加,重复这个过程直到结果只有一位数
代码如下:
# include <iostream> # include<cstdio> # include<cstring> # define LL long long using namespace std; int main() { char s[10005]; int n,temp,len,i; while(scanf("%s",s)) { if(strcmp(s,"0")==0) break; len =strlen(s); n=0; for(i=0;i<len;i++) { n += s[i]-'0'; } while(n>=10) { temp = n; n = 0; while(temp) { n += temp%10; temp /= 10; } } printf("%d\n",n); } return 0; }
相关文章推荐
- HDU 1013_Digital Roots(大数&模拟)
- hdu 1013 Digital Roots
- HDU 1013 Digital Roots 题解
- hdu 1013 Digital Roots(数论 模拟)
- hdu 1013 Digital Roots
- hdu1013 Digital Roots
- hdu 1013 Digital Roots 没注意细节,是1A不了水题的
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu-1013 Digital Roots
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- hdu 1013 Digital Roots
- hdu 1013 Digital Roots(字符输入,字符串处理)
- HDU's ACM 1013 Digital Roots
- hdu 1013 Digital Roots(简单题)
- HDU1013——Digital Roots
- hdu_1013_Digital Roots_必须用高精度
- hdu 1013 Digital Roots