Digital Roots
Background
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.
Example
Input
24
39
0
Output
6
3
意思是输入一个数,每个数各个数相加,如果小于10结束输出。大于10各位数再相加只到小于10.
用数学方法除9求余,能整除9的输出9
#include<iostream>
using namespace std;
int main()
{
int i,j;
cin>>i;
while(i!=0)
{
j=i%9;
if(j==0)
cout<<9<<endl;
else
cout<<j<<endl;
cin>>i;
}
return 0;
}
这个结果符合要求但是没通过,找到另外一种方法:用字符串来保存数字可以做到每个数字独立,且每个数字范围为0~9,而我一开始的想法就是输入一个数字的字符串数组,我计算所有数字的总和,然后再把这个和sum的每个数字再存入数字的字符串数组,后面的做法就跟前面一样了,而循环的终止条件就是总和sum<10.
#include<stdio.h>
int main()
{
int i,m;
char s[1000];
while(scanf("%s",s)==1&&s[0]!='0'){
for(m=i=0;s[i];i++)
m+=s[i]-'0';
printf("%d\n",m%9==0?9:m%9);
}
return 0;
}
这个代码可以通过
Digital Roots的更多相关文章
- Digital Roots 1013
Digital Roots 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:456 测试通过:162 描述 T ...
- Eddy's digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 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 ...
- HDU 1163 Eddy's digital Roots
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- ACM——Digital Roots
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间 ...
- HDOJ 1163 Eddy's digital Roots(九余数定理的应用)
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- Eddy's digital Roots(九余数定理)
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU1163 Eddy's digital Roots【九剩余定理】
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU 1013 Digital Roots(字符串)
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- HDU 1013.Digital Roots【模拟或数论】【8月16】
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
随机推荐
- 自定义NSLog
我们在调试程序的时候,往往需要输出一些日志信息,用到NSLog函数,当我们准备发布程序,需要注释掉NSLog代码,这个时候往往会定义一个宏,在调试的时候,会输出日志,在Release正式版本的时候,会 ...
- Windows Server 2008 R2 域控服务器运行nslookup命令默认服务器显示 UnKnown
一.问题: 域控服务器DOS窗口运行nslookup命令提示如下: 二.原因分析: 主要原因在于域控服务器的DNS服务器没有设置反向查找区域,计算机名称是通过IP地址反向查找到域控服务器的计算机名称. ...
- (转)HTTP 长连接和短连接
1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...
- GZIP压缩优化
使用gzip优化web应用(filter实现) 相关知识: gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户 ...
- 5.开发webservice
package com.atguigu.ws; import javax.jws.WebMethod; import javax.jws.WebService; /** * * @author Adm ...
- struts 文件上传
=================================== 实现单个文件上传 ====================================== 步骤1. 导入jar包: com ...
- Android 短信广播接收相关问题
本人是Android新手,最近做了一个关于监听手机短信功能的应用,我在网上看资料了解到广播分为有序广播和无序广播,有序广播:无序广播又称普通广播,其中的利弊我也一时没搞清楚,我用的是有序广播实现的,具 ...
- MVC5+EF6 入门完整教程
MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用 MVC5+EF6 入门完整教程10:多对多关联表更新&使用原生SQL@20150521 MVC5+EF6 入门完整教程9:多表 ...
- [已解决] 快速理解RSA算法
RSA算法基础详解 http://www.cnblogs.com/hykun/p/RSA.html RSA算法原理(一) http://www.ruanyifeng.com/blog/2013/06/ ...
- Logstash学习-plugin安装
Usage: bin/logstash-plugin [OPTIONS] SUBCOMMAND [ARG] ... Parameters: SUBCOMMAND subcommand [ARG] .. ...