joj1023
2011-08-29 09:07
190 查看
1023: Digital Roots
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 6304 | 1355 | Standard |
Background
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 isrepeated. 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
This problem is used for contest:
66 182
Submit /
Problem List / Status /
Discuss
Problem Set with Online Judge System Version 3.12
Jilin University
Developed by skywind,
SIYEE
#include<cstdio>
int add(int n)
{
int sum=0;
while(n>0)
{
sum+=n%10;
n=n/10;
}
return sum;
}
int out(int n)
{
if(n/10==0)return n;
else
{
return out(add(n));
}
}
void find(char a[])
{
int sum=0;
for(int i=0;a[i]!='\0';i++)
{
sum+=(a[i]-'0');
sum=out(sum);
}
printf("%d\n",sum);
}
int main()
{
char a[2000000];
while(1)
{
scanf("%s",a);
if(a[0]=='0')break;
find(a);
}
return 0;
}
相关文章推荐
- joj 1023: Digital Roots(WS超短)
- ACM&nbsp;JOJ&nbsp;1023
- SQL Server 2000:bcp.exe 参数的1023限制
- 关于JOJ 2042的启发——一种写程序计算幂函数的方法
- JOJ 1052: Square Destroyer解题报告
- acm 杭电 train problem ii 1023
- JOJ 1185: Knight Moves
- joj 1021题目
- pku 1023 The Fun Number Syst
- joj 2699: 16 and 18 博弈 最后一个取石子者输
- joj 2259 进制转换(含负进制)
- JOJ 2109 && POJ 1981 Circle and Points 计算几何 单位圆覆盖问题
- joj2075
- HDU 1023 卡特兰数
- JOJ 2724 hua rong dao (最小割 拆点)[无代码]
- joj1082
- joj2170
- JOJ 2474:Tile My Corridor 平铺棋盘 状态压缩解法
- joj 1317
- joj2699