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. 【CSS-移动端响应式布局详解】

    背景 移动端响应式布局开发主要方案有: 基于rem开发 基于媒体查询 基于弹性盒 基础概念 在讨论响应式布局知识前,先了解下移动端常用基础概念. 逻辑像素(CSS pixels) 浏览器使用的抽象单位 ...

  2. spring,springMVC,mybatis项目添加maven后报500错

    <resources> <resource> <directory>src/main/java</directory> <includes> ...

  3. 基于.net core 微服务的另类实现

    基于.net core 的微服务,网上很多介绍都是千篇一律基于类似webapi,通过http请求形式进行访问,但这并不符合大家使用习惯.如何像形如[ GetService<IOrderServi ...

  4. centos7字体中英文转化

    [root@localhost oracle]#vi /etc/locate.conf,把里面的内容改为: 转化为英文: LANG="en_US.UTF-8"LANGUAGE=&q ...

  5. Oracle特有函数 case when decode exists 分页rownum

    select * from EMP eselect * from dept dselect * from salgrade s--Oracle特有函数 case whenselect case 2 w ...

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

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

  7. CentOS6安装各种大数据软件 第十章:Spark集群安装和部署

    相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...

  8. jquery选择器基础

    简单选择器 类 id 元素/标签 * 复合(sel1,sel2)逗号隔开 层次选择器 s1 s2:后代选择器,空格隔开 p>c:子代选择器:不包括孙代及以下 p+next :相邻选择器 p~su ...

  9. Zookeeper -- 命令

    zkServer.sh start //启动zk进程 zkServer.sh stop //关闭zk进程 zkServer.sh status //查看zk状态 zkCli.sh //启动本地zk客户 ...

  10. S3 Zeta使用python和opencv

    一.为SDK的Buildroot打上下面的补丁 diff --git a/package/opencv/Config.in b/package/opencv/Config.in index c046b ...