Digital Roots
来源:互联网 发布:顶级域名多少钱 编辑:程序博客网 时间:2024/05/22 17:57
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解题报告: 这道题没有给出数据范围,说明数据可能很大(事实的确如此),用string取模即可;公式:(a+b)%n=(a%n+b%n)%n;code:#include<iostream>#include<algorithm>#include<stdio.h>#include<queue>#include<math.h>#include<string.h>#include<stdlib.h>using namespace std;int main(){ // freopen("input.txt","r",stdin); string s; while(cin>>s){ if(s[0]=='0') break; int sum=0,i=0; while(i<s.length()){ sum+=(s[i]-'0')%9; i++; } if(sum%9==0) cout<<"9"<<endl; else cout<<sum%9<<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
- File
- gulp详细入门教程
- JavaScript中的原型和继承
- Java线程池使用说明
- 结构体字节对齐总结
- Digital Roots
- linux之netstat
- 解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
- 欢迎使用CSDN-markdown编辑器
- 动态规划-采药问题 [原题+题解]
- 数据结构实验之栈八:栈的基本操作
- 百度地图集成(一)
- VO,PO,POJO的定义和区别
- 实现修改iOS应用程序名