Digital Roots
来源:互联网 发布:固定循环编程注意? 编辑:程序博客网 时间:2024/06/07 17:51
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 <iostream>#include <cstring>using namespace std;int main(){ int i,k,t; char a[1000]; while(cin>>a) { k=0; if(a[0]=='0') break; for(i=0;i<strlen(a);i++) k+=a[i]-'0'; t=k%9; if(t==0) cout<<9<<endl; else cout<<t<<endl; } return 0;}
阅读全文
0 0
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- PAT(Basic Level)_1012_数字分类
- Ducci Sequence
- java中的迭代器模式
- oracle常用
- [模板]manacher算法
- Digital Roots
- Ubuntu14.04下安装ros indigo opencv2.4.11
- JS学习(10)----DOM
- 南阳理工oj-水池数目
- PAT(Basic Level)_1013_数素数
- 程序员面试金典:机器人走方格II、魔术索引I
- Throwing cards away I
- 蓝牙加密三【Definition of Keys】
- 运算符