Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 53625    Accepted Submission(s): 16747

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
 
分析:
1  10  19(10)  28(10)  ....
2  11  20        29(11)
3  12  21        30
4  13  22       .....
5  14  23
6  15  24
7  16  25
8  17  26
9  18  27
 
#include<stdio.h>
#include<string.h>
int main(){
    long int sum,len,i;
    char num[10000];
    while(scanf("%s",num) && num[0]!='0'){
        
        len=strlen(num);
        sum=0;
        for(i=0;i<len;i++)
            sum+=num[i]-'0';
        sum=sum%9;
        if(sum>0)
            printf("%d\n",sum%9);
        else
            printf("9\n");
    }
    return 0;
}

杭电1013-Digitai Root(这是一道考研编程题-天大2015)的更多相关文章

  1. 杭电 1013 Digital Roots

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 反思:思路很简单,但是注意各位数加起来等于10的情况以及输入0的时候结束程序该怎么去表达 #in ...

  2. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  3. HDU 4639 hehe 杭电2013多校联赛第四场1008题

    解题报告:题目的意思是输入一个字符串,并规定,里面的“hehe”可以用"wqnmlgb"来代替,也可以不代替,问输入的这个字符串在经过相关的代替之后可以有多少种不同的形态.先打一个 ...

  4. 杭电oj1860:统计字符(字符串hash / 水题)

    统计字符 题目链接 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem D ...

  5. 杭电OJ2039——三角形(c++)(易错题:数据类型不确定)

    三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  6. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  7. 杭电 1272 POJ 1308 小希的迷宫

    这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...

  8. 杭电ACM题单

    杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...

  9. 用python爬取杭电oj的数据

    暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...

随机推荐

  1. java中的、标识符、运算符以及数据类型之间的转换。

    ---恢复内容开始--- 数据类型之间的转换: 1:自动转换:就是不用说出要转换成什么类型,由java中的虚拟机自动将小数据类型转换成大数据类型,但大数据中的数据精度有可能被破坏. 2:强制转换:强制 ...

  2. simple python code when @ simplnano

    code: import serial,time,itertools try: ser=serial.Serial(2,115200,timeout=0) except: print 'Open CO ...

  3. JS和JQUERY的区别

    ①.根据ID取元素 { JS:取到的是一个DOM对象. 例:var div = document.getElementByID("one"); JQUERY:取到的是一个JQUER ...

  4. 数据库SQL 查询

    查询 1.简单查询 select * from info(表名)   --查所有数据 select  code(列名),name(列名)  from 表名        --查指定列的数据 selec ...

  5. 当android studio一直显示gradle compile dependency

    出现这种情况,是被墙的问题,我的解决办法是这样的: 打开file---->setting,然后搜索gradle,把offline勾上,然后点击apply以及ok,就可以了. 有时候它会关闭,只需 ...

  6. IMapDocument interface

      Provides access to members that control the reading and writing of map document files.(提供访问的成员,控制读 ...

  7. 关于用php插入汉字到oracle中出现不乱码问题

    $conn = oci_pconnect("IBADMINTON", "5206576360", $db,'utf8'); 在代码中加入‘utf8’即可:   ...

  8. Logic and Fault simulation

    fault simulation是指对fault circuit的simulation,来locate manufacturing defects并且进行fault diagnosis. logic ...

  9. ThinkPHP讲解(一)框架基础

    ThinkPHP框架知识点过于杂乱,接下来将以问题的形势讲解tp(ThinkPHP的简写) 1.tp框架是什么,为什么使用是它? 一堆代码的集合,里边有变量.函数.类.常量,里边也有许多设计模式MVC ...

  10. JSP-04- 实现数据的保存

    .1  Session 一段时间内,单个客户与Web服务器的一连串相关的交换过程. Ø  4.1.1  应用的场景: 用户登录后保存用户状态 确定用户的唯一   Sessin.getId(); Ø  ...