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 ...
随机推荐
- C++ 中的sort排序用法
STL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:sort(begin,end ...
- python 调用封装好的模块
有些时候,我们写了些通用的模块,想调用的时候,该怎么操作呢? 以下是我写的一个简单的登录作为例子: 在cla.py中定义了一个Login_gues.pyt(带参数的实例):在cc.py下调用这个; 1 ...
- ios - block循环引用Demo示例
当实例变量中有了block属性,并且用copy来修饰,但是当调用block中的代码的时候,如果block中运用了self.属性的时候回造成循环引用. // // ViewController.h // ...
- AFNETWorking 不支持中文URL请求
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p ...
- css3(border-radius)边框圆角详解
传统的圆角生成方案,必须使用多张图片作为背景图案.CSS3的出现,使得我们再也不必浪费时间去制作这些图片了,只需要border-radius属性,支持浏览器IE 9.Opera 10.5.Safari ...
- csuoj 1117: 网格中的三角形
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1117 1117: 网格中的三角形 Time Limit: 3 Sec Memory Limit: ...
- Unit01: JAVA开发环境
Top JAVA开发环境 1. JAVA开发环境 1.1. 认识Linux操作系统 1.1.1. Linux的由来及发展 Linux起源于1991年,1995年流行起来,大家可以看到旁边的这个人,它就 ...
- 基础-DP
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like ...
- CSRF 攻击
一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...
- MXNet学习~试用卷积~跑CIFAR-10
第一次用卷积,看的别人的模型跑的CIFAR-10,不过吐槽一下...我觉着我的965m加速之后比我的cpu算起来没快多少..正确率64%的样子,没达到模型里说的75%,不知道问题出在哪里 import ...