HD1013Digital Roots
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
这题来自NewYork的题,其实一道数论题,有个小技巧,不过还是要用大数转换为字符串的方法,把每个数字相加,还是会很大.但这时可以用int了,接下来要用点数学知识:
/*
说明1.各位数字相加得到数x,这个x等于原来的数除以9的余数(原数为9的倍数除外)
2.若原数为9的倍数,最后结果就是9;
3.一个数字除以9的余数等于该数各数字之和除以9的余数。(这点为大数运算提供了极大的方便!)
*/
#include<iostream>
#include<string>
using namespace std;
int main(){
int sum,i,x;
char s[];
while(cin>>s && s[]!='')
{
sum = ;
for(i=;i<strlen(s);i++)
sum +=s[i]-'';
x = sum % ;
if(x==) x=;
cout<<x<<endl;
}
return ;
}
HD1013Digital Roots的更多相关文章
- gc roots 垃圾回收
gc roots包括以下几个: 虚拟机栈(栈桢中的本地变量表)中的引用对象 方法区中的类静态属性引用的对象 方法区中的常量引用的对象 本地方法栈中JNI(即native方法)的引用的对象 java,c ...
- Digital Roots 1013
Digital Roots 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:456 测试通过:162 描述 T ...
- 枚举GC Roots的实现
枚举根节点 从可达性分析中从GC Roots节点找引用链这个操作为例,可作为GC Roots的节点主要在全局性的引用(例如常量或类静态属性)与执行上下文(例如栈帧中的本地变量表)中,现在很多应用仅仅方 ...
- 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 ...
- HDU1013Digital Roots
G - Digital Roots Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- Gym 100818F Irrational Roots (数学)
Irrational Roots http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/F [题意]: 判断一个整系 ...
- HDU1013_Digital Roots【大数】【水题】
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1163 Eddy's digital Roots
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
随机推荐
- android 事件处理机制之requestDisallowInterceptTouchEvent
当手指触摸到屏幕时,系统就会调用相应View的onTouchEvent,并传入一系列的action.当有多个层级的View时,在父层级允许的情 况下,这个action会一直向下传递直到遇到最深层的Vi ...
- 【笨嘴拙舌WINDOWS】计时器精度
WINDOWS的大多数系统并非实时操作系统,所以不能规定计算机在某个精确到纳秒的时间让计算机做某项任务,如果规定了时间WINDOWS也将需要在完成了线程调度后,经行任务执行! 也就是说,如果你的应用程 ...
- short s1 = 1; s1 = s1 + 1;和 s1 += 1;
对于short s1 = 1; s1 = s1 + 1; 由于s1+1运算时会自动提升表达式的类型,所以结果是int型,再赋值给short类型s1时,编译器将报告需要强制转换类型的错误. 对于shor ...
- android studio sqlite操作代码片段
SQLiteDatabase db;Cursor cur; SimpleCursorAdapter adapter; // 打开或创建数据库db = openOrCreateDatabase(DB ...
- IOS中控制器的重要方法使用
1.屏幕即将旋转的时候调用(控制器监控屏幕旋转) - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfac ...
- Dom文档模型
文档对象模型 通过 JavaScript,您可以重构整个 HTML 文档.您可以添加.移除.改变或重排页面上的项目.要改变页面的某个东西,JavaScript 就需要获得对 HTML 文档中所 ...
- Mac 上安装MySQL
http://blog.neten.de/posts/2014/01/27/install-mysql-using-homebrew/ http://www.wkii.org/mac-os-x-ins ...
- android 语言切换过程分析
android 语言切换过程分析 2014-02-27 18:13 1207人阅读 评论(0) 收藏 举报 语言切换android语言切换android改变语言 最近在看一个bug,系统切换语言后,本 ...
- VPS技术介绍以及分析
VPS的全称为Virtual Private Server,叫做虚拟专用服务器(Godaddy称之为Virtual Dedicated Server,VDS).就是利用各种虚拟化手段把单台物理服务器虚 ...
- Spring中WebApplicationContext的研究
Spring中WebApplicationContext的研究 ApplicationContext是Spring的核 心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些 ...