Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 64420    Accepted Submission(s): 20053

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

交了七次才对。题意就是求数根,求法就是将数字每一位相加,若和为两位以上,继续相加,直至只有一位。若只有一位数,数根就是本身。否则重复求直到加出来的数只有一位。

此外引用一个九余数定理:

一个数对九取余后的结果称为九余数。

一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加)

代码:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int jisuan(const string &str)
{ int sum=0;
if(str.size()==1)
return sum=str[0]-'0';
for (int i=0; i<str.size(); i++)
{
sum=sum+(str[i]-'0')%9;//各位相加的和取模9,等于各位取模9后的和
}
return sum;
}
int main(void)
{
int n;
string str;
int num;
while (cin>>str&&str!="0")
{
num=jisuan(str);
while (num>=10)
{
string t;
ostringstream oin;
oin<<num;
t=oin.str();
num=jisuan(t);
}
if(num%9==0)//一直WA因为只判断了9而没有判断它的倍数
cout<<9<<endl;
else
cout<<num<<endl;
}
return 0;
}

HDU——1013Digital Roots(九余数定理)的更多相关文章

  1. HDOJ 1163 Eddy's digital Roots 九余数定理+简单数论

    我在网上看了一些大牛的题解,有些知识点不是太清楚, 因此再次整理了一下. 转载链接: http://blog.csdn.net/iamskying/article/details/4738838 ht ...

  2. Hdu1163 Eddy's digitai Roots(九余数定理)

    题目大意: 给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下: 例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6: 因为 6 < 10,所 ...

  3. hdoj-1013-Digital Roots(九余数定理)

    题目链接 #include <iostream> using namespace std; int main() { string a; int b; ") { b = ; ;i ...

  4. hdu 1163 Eddy's digital Roots 【九余数定理】

    http://acm.hdu.edu.cn/showproblem.php?pid=1163 九余数定理: 如果一个数的各个数位上的数字之和能被9整除,那么这个数能被9整除:如果一个数各个数位上的数字 ...

  5. HDOJ 1163 Eddy's digital Roots(九余数定理的应用)

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  6. Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  7. HDU-1163 Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. HDU-1163Eddy's digital Roots,九余定理的另一种写法!

    下午做了NYOJ-424Eddy's digital Roots后才正式接触了九余定理,不过这题可不是用的九余定理做的.网上的博客千篇一律,所以本篇就不发篇幅过多介绍九余定理了: 但还是要知道什么是九 ...

  9. HDU1163【九余数定理】【水题】

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. NGUI类之间的关系架构

    NGUI Drawcall 1.使用同一个altals的元素尽量放在同一个UIPanel下面,在NGUI中,它消耗的drawcall是以每个Panel为独立计算单位进行计算的. 2.如果一个UIPan ...

  2. Unity四元素运用之风向标跟随箭头

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class WindVane ...

  3. Dungeon Master的两种方法

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  4. 禁止DataGridView控件中添加和删除行

    实现效果: 知识运用: DataGridView控件的AllowUserToAddRows AllowUserDeleteRows和ReadOnly属性 实现代码: private void btn_ ...

  5. OO作业第三单元总结

    目录 一.JML语言理论基础及应用工具链 二.部署JMLUnitNG,自动生成测试用例 三.架构设计 第一次作业 第二次作业 第三次作业 四.Bug分析 五.心得体会 一.JML语言理论基础及应用工具 ...

  6. cocos2dx 使用XMLHttpRequest时回调status为0的问题

    今天使用cocos连接http访问时,使用XMLHttpRequest在pc上反问时正常的返回了status=0,但是在android上去返回status是0,看了一下底层代码, 发现status只有 ...

  7. matplotlib绘图(一)

    绘制这折现图 导入响应的包 import numpy as npimport pandas as pdfrom pandas import Series,DataFrame%matplotlib in ...

  8. Java-JFrame可视化开发

    Java-JFrame可视化开发的一般步骤 JFrame可以做出类似于QQ登录功能的窗体,通过JFrame可以利用Java代码实现窗体功能,一般用于CS项目的C(客户端)的开发: 利用JFrame可以 ...

  9. 【python】python安装和运行报错汇总

    本文主要用于汇总在python开发过程中遇到的各种环境.工具相关问题,便于后续遇到相关问题,及时搞定,持续更新. 一.安装pip失败,具体如下: 错误信息: python setup.py insta ...

  10. Python头脑风暴2

    今天想到了一个致富新途径:假如我在X东上班,我写个X宝爬虫,专门爬在X宝买奢侈品的土豪,然后我自己注册个X宝号,用脚本一个个加他们然后给他们发信息说我X东这还有比你更便宜更好的...不知道行不行啊(狗 ...