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. XML(可拓展标记语言)

      XML 是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词.短语或块成为 ...

  2. Android(java)学习笔记135:SQLite数据库(表)的创建 以及 SQLite数据库的升级

    一.数据库的创建 1.文件的创建      //引用,如果文件不存在是不会创建的   File  file = new File("haha.txt"):     //输出流写数据 ...

  3. Meaningful Mean

    You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K.a has N(N+1)⁄2 non- ...

  4. php面试相关

    22.描述一下大流量高并发量网站的解决方案 答: 1.确认服务器硬件是否足够支持当前的流量. 2.使用memcache缓存技术,将动态数据缓存到内存中,动态网页直接调用这些文件,而不必在访问数据库. ...

  5. Bootstrap 页面标题(Page Header)

    Bootstrap页面标题(PageHeader)是个不错功能,它会网页的标题的四周添加适当的间距,当一个网页中有多个标题并且每个标题之间需要添加一定适当的间距,使用页面标题是非常有用的.如果需要使用 ...

  6. 关于removeFromSuperview

    关于  - (void)removeFromSuperview 苹果官网API中是这么描述的: Unlinks the view from its superview and its window, ...

  7. logging console not work

    配置 log 信息传输到控制台 参考官网:https://docs.djangoproject.com/en/2.1/topics/logging/ By default, this config o ...

  8. 头文件string与string.h的区别

    在C++中,#include<iostream>与#include<iostream.h>的区别,前者要使用更新的编译器(其实大部分编译器多比较前卫了,出了有些搞嵌入式的用变态 ...

  9. C++ 学习笔记(二) const的加强

    const 含义为只读.如果在程序中显式改变const变量那么编译会报错. C语言的const: 在C语言中const 变量是放在内存中,如果使用指针可以间接改变const变量.所以在C语言中cons ...

  10. 初学者之 Git 和 Github

    git和github是两个完全不同的概念. git   是一个版本管理工具,是可以在你电脑不联网的情况下,只在本地使用的一个版本管理工具,其作用就是可以让你更好的管理你的程序,比如你原来提交过的内容, ...