题目1124:Digital Roots
来源:互联网 发布:网络安全技术是什么 编辑:程序博客网 时间:2024/05/18 02:08
- 题目描述:
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.
- 输入:
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.
- 输出:
For each integer in the input, output its digital root on a separate line of the output.
- 样例输入:
24390
- 样例输出:
63
- 提示:
The integer may consist of a large number of digits.
代码:
#include <stdio.h>char buf[100];int main() { while(scanf("%s",buf)!=EOF) { if(buf[0]=='0' && buf[1] == 0) break; int i,sum=0; for(i=0;buf[i]!=0;i++) sum += buf[i] - '0'; while(sum>9) { int n = sum; sum = 0; while(n!=0) { sum += n%10; n /= 10; } } printf("%d\n",sum); } return 0;}
输入的数要用字符串保存。
- 题目1124:Digital Roots
- 题目1124:Digital Roots
- 题目1124:Digital Roots
- 题目1124:Digital Roots
- 42-题目1124:Digital Roots
- 九度OJ 题目1124:Digital Roots
- 九度题目1124Digital Roots
- 题目1124:Digital Roots (方法超简单)
- 【九度】题目1124:Digital Roots
- 九度OJ题目1124:Digital Roots
- 题目1124:Digital Roots 九度OJ
- 九度 题目 1124:Digital Roots
- 九度题目1124:Digital Roots
- 题目42:Digital Roots
- 【九度OJ】题目1124:Digital Roots 解题报告
- 1124:Digital Roots
- Num 14: HDOJ: 题目1013 : Digital Roots
- HDU OJ Digital Roots 题目1013
- ubuntu 安装svn
- 设备驱动模型
- hdu 4630 No Pain No Game 线段树 树状数组
- VC学习之类的类成员初始化
- codeforces 313C. Ilya and Matrix
- 题目1124:Digital Roots
- 走马灯(四个方格循环移动)
- 2013 Multi-University Training Contest 3
- 字符集和字符编码
- Linux下的几个关机命令
- 匿名对象的简单示例
- C# XML parsing 去掉 namespace 声明
- TCP/IP详解学习笔记(5)
- NDK开发环境