C - Digital Roots

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

题目描述:多组输入,输入n,把n的每一位加起来,求出新的n来,不断地加,直到n小于10.用到九余定理。

#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
while (cin>>s) {
int ans = 0;
int lens = s.size();
for (int i = 0; i < lens; i++) {
ans += s[i] - '0';
}
if (ans == 0)break;
else
cout << (ans- 1) % 9 + 1 << endl; //九余定理
}
return 0;
} /*
设x是一个四位数
x=a*1000+b*100+c*10+d => x=a*999+b*99+c*9+d+a+b+c =>要求a+b+c+d 即求 (a*999+b*99+c*9+d+a+b+c)%9即可, 即求x%9
特例x==9时,,所以先-1,取余9后再+1
例如45=4+5=9
如果9%9==0 但是结果是0
*/

Digital Roots:高精度的更多相关文章

  1. Digital Roots 1013

    Digital Roots 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:456            测试通过:162 描述 T ...

  2. Eddy's digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  3. Digital Roots 分类: HDU 2015-06-19 22:56 13人阅读 评论(0) 收藏

    Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. HDU 1163 Eddy's digital Roots

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

  5. ACM——Digital Roots

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间 ...

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

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

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

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

  8. HDU1163 Eddy&#39;s digital Roots【九剩余定理】

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

  9. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

随机推荐

  1. 使用interface与类型诊断机制判断一个类型是否实现了某个方法

    Golang中的interface通常用来定义接口,在接口里提供一些方法,其他类型可以实现(implement)这些方法,通过将接口指针指向不同的类型实例实现多态(polymorphism),这是in ...

  2. iOS:cocoapods 配置相关(19-04-02更)

    1.gem sources 2.libwebp 1.gem sources 因为,mac更新,cocoapods也要更新,使用下面指令,提示找不到.org,原因是淘宝的镜像源.org换成.com,所以 ...

  3. 给大家推荐一款非常好用的表单验证插件:lr-verify.js

    废话不说,直接上代码说明,1分钟学会: 例: 1.验证配置 $.extend(Verify.types, { "must" : { "verify" : fun ...

  4. ie浏览器报 promise 问题

    1.首先安装:babel-polyfill    npm install babel-polyfill --save2.然后引入:babel-polyfill 在build目录下,webpack.ba ...

  5. GBK 文件在 sublime 保存时被强制保存为 utf-8 导致中文乱码, 恢复。

    原来在 CoverteToUTF8 的 README.zh_CN.md 文件里就有解决方法,如下: * 问:我的文件被保存为 UTF-8,而且变成了乱码,要如何恢复? 答:请打开这个文件,并确认它的编 ...

  6. BZOJ 4520: [Cqoi2016]K远点对(k-d tree)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1162  Solved: 618[Submit][Status][Discuss] Descripti ...

  7. jQuery属性操作之.attr()

    目录 .attr() 调用形式:$("xxx").attr(name) 调用形式:$("xxx").attr(name,value); 调用形式:$(" ...

  8. 针对shiro框架authc拦截器认证成功后跳转到根目录,而非指定路径问题

    一.针对shiro框架authc拦截器认证成功后跳转到根目录,而非指定路径问题 首先,我们先来了解一下authc登录拦截器工作原理 authc拦截器有2个作用: 1>登录认证     请求进来时 ...

  9. 一图看懂hadoop Spark On Yarn工作原理

    hadoop Spark On Yarn工作原理

  10. 搭建iSCSI共享IPSAN

    iSCSI(internet SCSI)是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速千兆以太网上进行路由选择. ...