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. 反编译android APK

    我们经常会在如下的情况使用反编译 1.看到别人应用中的酷炫功能,想知道是如何实现的 2.别人应用的素材排版好漂亮,想套用模仿   百度一下就已经有一大堆反编译的教程了,我还是坚持学习记录一下.   A ...

  2. IIS7或者IIS7.5部署MVC项目时出现404错误

    IIS7或者IIS7.5部署MVC项目时出现404错误 服务器上需要安装Windows 补丁 kb980368  下载链接:http://support.microsoft.com/kb/980368

  3. HDU 1505 City Game

    这题是上一题的升级版 关键在于条形图的构造,逐行处理输入的矩阵,遇到'F'则在上一次的条形图基础上再加1,遇到'R'则置为0 然后用上一题的算法,求每行对应条形图的最大矩阵的面积. 另外:本来是deb ...

  4. KVC&KVO&NSNotification

    KVC,即是指 NSKeyValueCoding,一个非正式的 Protocol,提供一种机制来间接访问对象的属性.KVO 就是基于 KVC 实现的关键技术之一. 一个对象拥有某些属性.比如说,一个 ...

  5. 通过运行时动态给OC分类添加属性

    #import <UIKit/UIKit.h> /** iOS 开发中,分类默认不允许保存属性 如果在分类中,定义一个属性,需要自己实现 getter & setter 方法,而且 ...

  6. Sublime 的中文乱码问题

    Sublime Text 是现在最受欢迎的文本编辑器,没有之一.它非常简洁,而且对各种代码的高亮显示很美观.但是,它默认不支持 GBK.Shift-JIS 等中文.日本编码格式,故打开此类文件会出现乱 ...

  7. php的类型约束

    //如下面的类 class MyClass { /** * 测试函数 * 第一个参数必须为 OtherClass 类的一个对象 */ public function test(OtherClass $ ...

  8. .htaccess 文件的使用

    用于服务器对文件夹的控制 官方解释为 :分布式配置文件 ,提供了针对目录改变配置的方法; 项目可以有多个这样的配置文件,子目录文件会覆盖父目录的配置 在apache(这里泛指服务器)中,/conf/v ...

  9. 为什么要用Java泛型

    啥是泛型? 泛型(generic)是指参数化类型的能力.可以定义带泛型类型的类或方法,随后编译器会用具体的类型来代替它. 举个栗子 上述代码在编译期没有问题,但在运行期,将会报错.就是因为List的a ...

  10. JBPM4入门——8.等待节点的分支执行

    JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...