HDU1013
来源:互联网 发布:python高级编程 编辑:程序博客网 时间:2024/05/16 15:04
Digital Roots
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//HDU1013 基础题//Digital Roots//2017.05.24 by wyj#include#include using namespace std;int main(){int dr;string s;int w[100];while (cin >> s, (s != "0")){dr = 0;for (int k = 0;s[k] != '\0';k++)//求输入的整数(字符串)各位之和dr{w[k] = s[k] - '0';dr += w[k];}while (!(dr >= 0 && dr <= 9))//如果dr不是一位数,再求各位之和保存为dr{int i = 0;int t = dr;dr = 0;while (t){w[i] = t % 10;dr += w[i];t /= 10;i++;}}cout << dr << endl;}}
阅读全文
0 0
- hdu1013
- hdu1013
- hdu1013
- hdu1013
- HDU1013
- HDU1013
- HDU1013
- hdu1013
- HDU1013
- HDU1013
- HDU1013
- HDU1013
- hdu1013!【水题】
- hdu1013(基础)
- HDU1013 Digital Roots
- hdu1013 Digital Roots
- hdu1013 Digital Roots
- hdu1013(Digital Roots)
- android开机动画源文件配置
- [codechef]WEASELTX
- hdu 4398 X mod f(x)(数位DP)
- 把一个对象的属性值拷贝到另一个对象上去
- setup time的一些思考点
- HDU1013
- 例题6-16 单词(Play On Words, UVa 10129)
- mysql_数据完整性
- 卡尔曼滤波算法分析
- HDU 4821 字符串哈希
- JavaScript的数组,函数,对象
- Elasticsearch之分析(analysis)和分析器(analyzer)。
- HDU1014
- 信息熵计算(自己编写的python代码,垃圾,高手绕道)