HDU--1013--Digital Roots
2017-07-05 09:53
381 查看
Description
The digital rootof a positive integer is found by summing the digits of the integer. If theresulting value is a single digit then that digit is the digital root. If theresulting
value contains two or more digits, those digits are summed and theprocess 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 avalue of 6. Since 6 is a single digit, 6 is the digital root of 24. Nowconsider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 isnot a single digit, the process
must be repeated. Adding the 1 and the 2 yeilds3, a single digit and also the digital root of 39.
Input
The input filewill contain a list of positive integers, one per line. The end of the inputwill be indicated by an integer value of zero.
Output
For each integerin the input, output its digital root on a separate line of the output.
Sample Input
24
39
0
Sample Output
6
3
题意:求各位上的数相加
注意,数位很长,虽然题上没说,但是通过测试可以看出来
一:取余那个不太懂
二:自己的方法,先加一次
The digital rootof a positive integer is found by summing the digits of the integer. If theresulting value is a single digit then that digit is the digital root. If theresulting
value contains two or more digits, those digits are summed and theprocess 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 avalue of 6. Since 6 is a single digit, 6 is the digital root of 24. Nowconsider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 isnot a single digit, the process
must be repeated. Adding the 1 and the 2 yeilds3, a single digit and also the digital root of 39.
Input
The input filewill contain a list of positive integers, one per line. The end of the inputwill be indicated by an integer value of zero.
Output
For each integerin the input, output its digital root on a separate line of the output.
Sample Input
24
39
0
Sample Output
6
3
题意:求各位上的数相加
注意,数位很长,虽然题上没说,但是通过测试可以看出来
一:取余那个不太懂
#include <stdio.h> int main() { int s,i; char n[2000]; while(~scanf("%s",n)) { if(n[0]=='0') break; s=0; for(i=0;n[i];i++) { s=s+n[i]-'0'; if(s>=10) s=s/10+s%10; } printf("%d\n",s); } }
二:自己的方法,先加一次
#include <stdio.h> int main() { int s,i; char n[2000]; int m; while(~scanf("%s",n)) { if(n[0]=='0') break; s=0; for(i=0;n[i];i++) { s=s+n[i]-'0'; } m=s; s=0; while(1) { if(m<=0&&s>=10) { m=s; s=0; } if(m<=0&&s<10) break; s=s+m%10; m=m/10; } printf("%d\n",s); } }
相关文章推荐
- hdu1013 Digital Roots
- hdu 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU 1013 Digital Roots
- POJ 1519 && 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 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【数根计算 余九解法】