HDU-1013 Digital Roots
2016-01-26 00:34
447 查看
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
题意:水题,求一个数的各个位的数的和sum,如果sum>9,则重复这个求位数和的过程,该题没有给出具体数据范围,只说明了是一个正整数,用字符串存储输入的大整数,sum则直接用整型处理即可。
AC代码:
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
题意:水题,求一个数的各个位的数的和sum,如果sum>9,则重复这个求位数和的过程,该题没有给出具体数据范围,只说明了是一个正整数,用字符串存储输入的大整数,sum则直接用整型处理即可。
AC代码:
#include <iostream> #include <cstring> #include <string> using namespace std; #define LL long long int main() { LL n,sum; string str; while(cin>>str&&str!="0") { sum=0; for(int i=0;i<str.length();++i) { sum+=str[i]-'0'; } while(sum>=10) { n=0; while(sum) { n+=(sum%10); sum/=10; } sum=n; } cout<<sum<<endl; } return 0; }
相关文章推荐
- 构建现实机器学习系统的其他10点经验(二)
- 传智播客168期JavaEE就业班(第一天 html)
- 正式发表--java学习第二天,变量等信息。
- 20160123.CCPP详解体系(0002天)
- [置顶] LibGDX教程——重建Flappy Bird——(8)屏幕切换与播放声音(终结)
- 第一个只出现一次的字符位置
- Ubuntu 配置无密码访问的samba服务器
- NBUT 1457 Sona(莫队算法)
- Java设计模式-策略模式(Strategy Pattern)
- 修改HOST来登录Goole
- 【个人吐槽慎入】:别纠结于`工具`,感谢Windows
- LeetCode 71. Simplify Path
- 运维小知识之nginx---CentOS6.5安装nginx配置nginx sticky
- 4.9 Routing -- Query Parameters
- 简明python教程笔记
- 几种不同的多路径软件查看多路径状态的方法
- MiUI手机疯狂输出logcat处理
- 历届蓝桥杯C/C++省赛试题
- mainstoryboard.storyboard 视图为什么变成代码?如何恢复?
- [C#解惑] #1 在构造函数内调用虚方法