杭电1013
来源:互联网 发布:java不关闭流 编辑:程序博客网 时间:2024/06/07 16:29
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
24
39
0
Sample Output
6
3
开始一看,认为很简单,把各个位上的元素相加便可以得到答案,可是这是一个大数,不能用简单的排序,用string找不到位数,后面百度,直接求对于9的余数便可,有规律啊!!!所以以后不能急着做题,先思考。。。
<span style="color:#000099;">#include <iostream>#include<string.h>using namespace std;int main(){ int b=0,a; char h[10001]; while(cin>>h) { if(strcmp(h,"0")==0)break; for(a=0;a<strlen(h);a++) b+=h[a]-'0';//对int下的书求和 while(b>9) b%=9; if(b==0) cout<<9<<endl; else cout<<b<<endl; b=0; } return 0;}</span>
0 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了
- 白话经典算法系列之七 堆与堆排序
- python学习——进程vs线程
- Spark性能优化:资源调优篇
- 数据库的隔离级别
- 字符串和数字之间的转换
- 杭电1013
- uImage在内存中无法正常启动——UBoot-2010.06在TQ2440上的移植--机器码配对
- DBUtil连接池
- 判断二分图——染色法
- java 集合框架
- 【Codeforces 678E】【JZOJ 4648】锦标赛
- SPI flash debug trace
- js基础汇总
- 283. Move Zeroes