Digital Roots


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

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

 





Source

Greater New York 2000

 





Recommend

We have carefully selected several similar problems for you:  1017 1021 1048 1016 1019

题目大意:给你一个数,若这个数的各个位数上的和为一位数字,则输出结果。

否则继续计算上一结果

的各个位数上的和。知道结果为一位数字,进行输出

思路:本以为九余数定理直接水过。发现64位数也不行~果断大数A过

#include<stdio.h>
#include<string.h>
char num[110];
int main()
{
while(~scanf("%s",num) && num[0]!='0')
{
int len = strlen(num);
int sum = 0;
for(int i = 0; i < len; i++)
{
sum += num[i] - '0';
} while(sum > 9)
{
int a = 0;
while(sum > 0)
{
a += sum % 10;
sum /= 10;
}
if(a != 0)
sum = a;
} printf("%d\n",sum);
}
return 0;
}

HDU1013_Digital Roots【大数】【水题】的更多相关文章

  1. HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  2. POJ 2506 Tiling dp+大数 水题

    大致题意:现有两种方块(1X2,2X2),方块数量无限制.问用这两种方块填满2Xn的矩阵的填法有多少种. 分析:通俗点说,找规律.专业化一点,动态规划. 状态d[i],表示宽度为i的填法个数. 状态转 ...

  3. HDU-1047-Integer Inquiry(Java大数水题 &amp;&amp; 格式恶心)

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

  4. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  5. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  7. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  8. 每日一刷(2018多校水题+2016icpc水题)

    11.9 线段树 http://acm.hdu.edu.cn/showproblem.php?pid=6315 求逆序对个数 http://acm.hdu.edu.cn/showproblem.php ...

  9. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  10. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

随机推荐

  1. hdu 2459 (后缀数组+RMQ)

    题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...

  2. cocos2d - CCParallaxNode 例子

    CGSize winSize = [[CCDirector sharedDirector] winSize]; CCParallaxNode * node = [CCParallaxNodenode] ...

  3. kmeans算法的matlab实践

    把图像中所有的像素点进行RGB聚类分析,然后输出看结果 img = imread('qq.png'); %取出R矩阵,并将这个R矩阵拉成一列 imgR = img(:,:,1); imgR = img ...

  4. 【原】Storm学习资料推荐

    4.Storm学习资料推荐 书籍: 英文: Learning Storm: Ankit Jain, Anand Nalya: 9781783981328: Amazon.com: Books Gett ...

  5. Designing Evolvable Web API with ASP.NET 随便读,随便记 “The Internet,the World Wide Web,and HTTP”

    1982年,诞生了 Internet; 1989年,诞生了World Wide Web . "World Wide Web"的构造为主要由 三部分构成: resources 资源 ...

  6. https://github.com/mlzboy/spider-impl.git

    Installation - pygit2 0.24.0 documentation Python 2.7, 3.2+ or PyPy 2.6+ (including the development ...

  7. JavaFx2.0中CSS的应用

    http://user.qzone.qq.com/773534839#!app=2&via=QZ.HashRefresh&pos=1326994508 ———————————————— ...

  8. Sqoop 命令

    1)list-databases     List available databases on a server sqoop list-databases --connect jdbc:db2:// ...

  9. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  10. HDU 4884 TIANKENG’s rice shop (模拟)

    TIANKENG's rice shop 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/J Description TIANKE ...