杭电 #1013
来源:互联网 发布:青岛永诚网络 编辑:程序博客网 时间:2024/06/16 12:18
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 78199 Accepted Submission(s): 24461
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.
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
解析:这是一道求正数根的问题,唯一有问题的地方就是可能不会考虑到这是个大数题。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){ char s[10000]; while(cin>>s) { int n=0,t,m; if(strcmp(s,"0")==0) break; int len=strlen(s); for(int i=0;i<len;i++) { n+=s[i]-'0'; while(n>=10) { t=n%10; m=n/10; n=t+m; } } printf("%d\n",n); } return 0;}
- 杭电1013题
- 杭电1013
- 杭电 1013
- 杭电 ACM 1013
- 杭电 1013
- 杭电 1013
- 杭电acm 1013
- 杭电oj 1013
- 杭电1013
- 杭电1013
- 杭电 #1013
- 杭电1013的疑问?
- 杭电 1013 Digital Roots
- 杭电1013 Digital Roots
- 杭电ACM HDU 1013
- Digital Roots 杭电1013
- 杭电1013 Digital Roots
- 杭电1013 AC了
- [py]c和java笔记语法对比
- Ubuntu fcitx输入法无法显示候选词
- 为项目编写start.sh,stop.sh
- ArrayAdapter模板
- C++引用理解
- 杭电 #1013
- 关于这一切
- 通信方式--信号量的一系列操作(创建 获取 设置 删除以及PV操作)
- JavaScript 各种遍历方式详解
- github上创建java项目简单操作
- opencv的mat操作
- 旋转矩阵、欧拉角、四元数理论及其转换关系
- python装饰器
- 【Bugku】这是一张单纯的图片?? 【writeup】