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. Filezilla中文字符文件看不到或显示乱码的解决办法

    Filezilla确实是跨平台的好软件,可之前我就在ubuntu下郁闷为什么看坛子FTP里竟然是空的.最近换MAC版的FZ结果还是这样就奇怪了. 后来想Filezilla应该是支持字符集转换的,所以在 ...

  2. 【Android】如何使用安卓的logcat『整理』

    logcat是Android中一个命令行工具,可以用于得到程序的log信息.开发调试和测试定位bug都挺有用哒 有两种方式可以达到查看log的目的. 一 Eclipse集成DDMS插件 1 安装ecl ...

  3. 取requests返回字典值用json()

    python模块requests返回值用json()["h"][key]可以取出下面的value

  4. OnItemClickListener 的参数详解(转)

    转载地址:http://blog.iamzsx.me/show.html?id=147001 我们在使用ListView的时候,一般都会为ListView添加一个响应事件android.widget. ...

  5. Junit3.8 Stack测试

    package test; public class MyStack { private String[] elements; private int nextIndex; public MyStac ...

  6. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

  7. Web Service学习之九:Restful WebService

    1.Rest 2.Rest webService 3.SpringMVC Restful 参考: http://spring.io/guides/gs/rest-service/ http://www ...

  8. JDBC学习笔记(4)——PreparedStatement的使用

    PreparedStatement public interface PreparedStatement extends Statement;可以看到PreparedStatement是Stateme ...

  9. 【转】在企业内部分发 iOS 应用程序

    (via:破船之家,原文:Provision iOS IPA App for In-House Enterprise Distribution)   在企业内部分发 iOS 应用程序非常复杂.经过努力 ...

  10. PC问题-VMware Workstation出现“文件锁定失败”

    问题现象:电脑关机时挂起VMware Workstation后,第二天运行VMware Workstation出现“文件锁定失败”. 问题原因:在WIN的目录中有*.LCK文件,此文件是用来锁定当前虚 ...