HDU1013_Digital Roots【大数】【水题】

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;
}
时间: 2024-10-11 16:58:27

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

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

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 64256    Accepted Submission(s): 18286 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in o

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

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14886    Accepted Submission(s): 3811 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He e

HDU 1013 Digital Roots(水题)

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 57919    Accepted Submission(s): 18100 Problem Description The digital root of a positive integer is found by summing the digits of

大数--几道水题,用来学学JAVA

几道水题,练习一下JAVA写大数 poj2305  Basic remains 大数可以直接用非十进制读入,读入的数在变量中是十进制的 输出的时候要先用BigInteger的toString方法转换为相应的进制 1 import java.math.*; 2 import java.util.*; 3 class Main 4 { 5 public static void main(String[] args) 6 { 7 Scanner cin = new Scanner(System.in)

poj水题-1001 一个简单的大数乘幂算法实现

说到底就是一个大数乘幂运算,小数点后零.明白大数乘幂算法直接搞. 这里就有几个问题: 1.幂位数小可以用二进制容器表示(取模更好,但我是为了练习STL) 2.n位大数用string表示,外加一个int型表示小数点位置 3.字符串×字符串用小学竖式乘法算法就行,注意补零.位数多时两个string两个string地加. 代码长,但理解容易,大数乘法,加法函数很多都能重用. 1 #include <iostream> 2 #include <vector> 3 #include <

HDU1250_Hat&#39;s Fibonacci【大数】【水题】

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7854    Accepted Submission(s): 2551 Problem Description A Fibonacci sequence is calculated by adding the previous two members th

HDU 4006 The kth great number (基本算法-水题)

The kth great number Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too mu

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

HDU 5832 A water problem(某水题)

HDU 5832 A water problem(某水题) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b