HDU1013 POJ1519 UVALive2085 Digital Roots【数根+水题】

Digital Roots

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


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

Regionals 2000 >> North America - Greater NY



问题链接:HDU1013 POJ1519 UVALive2085 Digital Roots。

问题简述:输入若干正整数,求其数根,直到输入为0为止。

问题分析

  方法一:数根是指整数的各个位的数字之和。如果其和为1位整数,则为结果;如果其和为多位整数,则再将各位数字相加,直到其和为1位数为止。这个问题的大陷阱是,没有指出整数是多少位的。即使使用unsignde long long类型,也可能会溢出的。所以,需要按字符串来处理。

  方法二:一边读入数据一边处理,省去字符串数组

  方法三:该问题的最佳解法是利用数论的9余数定理来计算数根。一个数的数根等于该数的9的余数,若余数为0则结果为9。

程序说明:(略)

题记

  一边读入数据一边处理,省去字符串数组,是最佳的做法。

  玩程序玩的就是空间和时间。


AC的C语言程序如下(方法三):

/* HDU1013 POJ1519 Digital Roots */

#include 

int main(void)
{
    char in;
    int digitroot;

    for(;;) {
        // 读入一个字符
        in = getchar();

        // 判断结束条件
        if(in == '0')
            break;

        // 计算数根
        digitroot = 0;
        while(in != '\n') {
            // 利用9余数定理和数论的余数定理进行计算
            digitroot += in - '0';
            digitroot = digitroot % 9;

            in = getchar();
        }

        // 输出结果
        printf("%d\n", (digitroot == 0) ? 9 : digitroot);
    }

    return 0;
}


AC的C语言程序如下(方法二):

/* HDU1013 POJ1519 Digital Roots */

#include 

int main(void)
{
    char in;
    int digitroot;

    for(;;) {
        // 读入一个字符
        in = getchar();

        // 判断结束条件
        if(in == '0')
            break;

        // 计算数根
        digitroot = 0;
        while(in != '\n') {
            // step1 计算各位数字之和
            digitroot += in - '0';

            // step2 每个10都变为1
            digitroot = digitroot / 10 + digitroot % 10;

            in = getchar();
        }

        // 输出结果
        printf("%d\n", digitroot);
    }

    return 0;
}


AC的C语言程序如下(方法一):

/* HDU1013 POJ1519 Digital Roots */

#include 

int main(void)
{
    char s[1024], *p;
    int digitroot;

    while(~scanf("%s",s)) {
        // 判断结束条件
        if(s[0] == '0')
            break;

        // 计算数根
        p = s;
        digitroot = 0;
        while(*p) {
            // step1 计算各位数字之和
            digitroot += *p++ - '0';

            // step2 每个10都变为1
            digitroot = digitroot / 10 + digitroot % 10;
        }

        // 输出结果
        printf("%d\n", digitroot);
    }

    return 0;
}



你可能感兴趣的:(#,ICPC-备用二,#,ICPC-POJ,#,ICPC-HDU,#,ICPC-HDU水题,#,ICPC-UVALive)