Digital Roots(1013)
来源:互联网 发布:南通水立方js漂亮吗 编辑:程序博客网 时间:2024/05/21 22:32
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49082 Accepted Submission(s): 15273
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.
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
63
-----------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int fun(int);
int main()
{
char a[1005];
int i,l,sum;
while(scanf("%s",a),a[0]!='0')
{
l = strlen(a);
sum = 0;
for(i=0;i<l;i++)
sum+=a[i]-'0';
while(sum>9)
sum = fun(sum);
printf("%d\n",sum);
}
return 0;
}
int fun(int x)
{
int y =0;
while(x!=0)
{
y+=x%10;
x=x/10;
}
return y;
}
0 0
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- 1013:Digital Roots
- hdu 1013 Digital Roots
- HDOJ 1013 Digital Roots
- HDU 1013 Digital Roots
- 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
- HDOJ 1013 Digital Roots
- hdu 1013 Digital Roots
- 1013 Digital Roots
- 1013 Digital Roots
- hdu 1013 Digital Roots
- E - Roads in the North
- Python模块——_winreg操作注册表
- VS2010/MFC编程入门之二(利用MFC向导生成单文档应用程序框架)
- 轻松搞定面试中的二叉树题目
- MySql学习笔记5:正则表达式搜索
- Digital Roots(1013)
- 线程互斥锁的问题(线程中使用定时器信号)
- louboutin france Avant rencontre La nation
- linux c/c++ 获取时间
- escarpins noirs pas cher « Avant cette
- oracle 10g 安装成功之后,scott用户被锁定问题解决
- 这个社会缺什么,我们就创造什么——一些个人的想法
- linphone android源码编译 msilbc出错解决
- 平衡二叉树(AVL)的实现,附可运行C语言代码