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的更多相关文章

  1. gc roots 垃圾回收

    gc roots包括以下几个: 虚拟机栈(栈桢中的本地变量表)中的引用对象 方法区中的类静态属性引用的对象 方法区中的常量引用的对象 本地方法栈中JNI(即native方法)的引用的对象 java,c ...

  2. Digital Roots 1013

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

  3. 枚举GC Roots的实现

    枚举根节点 从可达性分析中从GC Roots节点找引用链这个操作为例,可作为GC Roots的节点主要在全局性的引用(例如常量或类静态属性)与执行上下文(例如栈帧中的本地变量表)中,现在很多应用仅仅方 ...

  4. Eddy's digital Roots

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

  5. 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 ...

  6. HDU1013Digital Roots

    G - Digital Roots Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   ...

  7. Gym 100818F Irrational Roots (数学)

    Irrational Roots http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/F [题意]: 判断一个整系 ...

  8. HDU1013_Digital Roots【大数】【水题】

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

  9. HDU 1163 Eddy's digital Roots

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

随机推荐

  1. visual studio 2015常用快捷键

    常用快捷键 技巧 0.0 删除文件中的当前行: Home + Shife-End + Delete 技巧 1.1 避免意外复制一个空白行 工具->选项->文本编辑器->所有语言-&g ...

  2. JAVA字符串格式化-String.format()的使用 (转载)

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

  3. BZOJ 1984 月下“毛景树”

    我觉得我要把BZOJ上的链剖写完了吧.... #include<iostream> #include<cstdio> #include<cstring> #incl ...

  4. I.MX6 ov5640 camera

    /************************************************************************ * I.MX6 ov5640 camera * 说明 ...

  5. 【英语】Bingo口语笔记(34) - Hit系列

    hit it off 合得来 hit the bottle 喝醉酒 hit the spot 正合要求,恰到好处

  6. int (*(*fp)(void *))[10]; 指向函数的指针类型

    <pre lang="c" escaped="true">int (*(*fp)(void *))[10]; //这个类型用typedef分解出来 ...

  7. 在centOS中加入本地ISO yum源

    注:本文转载自<liujun_live的博客>,感谢原博主的辛勤写作:原文地址:http://blog.sina.com.cn/s/blog_8ea8e9d50101em6f.html 在 ...

  8. Android 校验apk文件渠道号、包名、版本号

    功能:可查看单个或目录下所有apk文件的渠道号.包名.版本号 下载地址:http://download.csdn.net/detail/zgz345/9248487使用:以查看包名.版本号为例 1.c ...

  9. web-3g-(163)网易-邮箱-记事本-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-cnblogs(博客园)-数据库设计-2,Admin(用户后台) DatabaseName:cnblogs(博客园) Model:Admi ...

  10. YII Framework学习教程-YII的国际化

    一个web应用,发布到互联网,就是面向全球用户.用户在世界的各个角落都可以访问到你的web应用,当然要看你的网站和不和谐,不和谐的web应用在和谐社会是不让你访问的. YII提供了国际化的支持,可以让 ...