UVa 11332 Summing Digits (water ver.)

11332 - Summing Digits

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2307

For a positive integer n, let f(n) denote the sum of the digits of n when represented in base 10. It is easy to see that the sequence of numbers n, f(n), f(f(n)), f(f(f(n))), ... eventually becomes a single digit number that repeats forever. Let this single digit be denoted g(n).

For example, consider n = 1234567892. Then:

f(n) = 1+2+3+4+5+6+7+8+9+2 = 47
f(f(n)) = 4+7 = 11
f(f(f(n))) = 1+1 = 2

Therefore, g(1234567892) = 2.

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45521.htm

Each line of input contains a single positive integer n at most 2,000,000,000. For each such integer, you are to output a single line containing g(n). Input is terminated by n = 0 which should not be processed.
Sample input

2
11
47
1234567892
0

Output for sample input

2
2
2
2

完整代码:

/*0.016s*/

#include<cstdio>
#include<cstring>  

char n[15];  

int main()
{
    int i, sum, len;
    while (gets(n), n[0] != '0')
    {
        sum = 0;
        len = strlen(n);
        for (i = 0; i < len; ++i)
            sum += n[i] & 15;
        sum = sum / 10 + sum % 10;
        sum = sum / 10 + sum % 10;
        printf("%d\n", sum);
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索input
, include
, sum
, integer
, 10
single
summing、summing up、summingbird、the summing up、summing混音,以便于您获取更多的相关知识。

时间: 2024-11-02 22:51:29

UVa 11332 Summing Digits (water ver.)的相关文章

UVa 11727 Cost Cutting (water ver.)

11727 - Cost Cutting Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2827 Company XYZ have been badly hit by recession and is taking a lot of cost cutting

UVa 10300 Ecological Premium (water ver.)

10300 - Ecological PremiumTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=1241 German farmers are given a premium depending on the conditions at their far

UVa 490 Rotating Sentences (water ver.)

490 - Rotating Sentences Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=431 In ``Rotating Sentences,'' you are asked to rotate a series of input sentence

UVa 445 Marvelous Mazes (water ver.)

445 - Marvelous Mazes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=386 Your mission, if you decide to accept it, is to create a maze drawing program. A

UVa 488 Triangle Wave (water ver.)

488 - Triangle Wave Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=429 In this problem you are to generate a triangular wave form according to a specifie

UVa 10783 Odd Sum (water ver.)

10783 - Odd Sum Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1724 Given a range [a, b], you are to find the summation of all the odd integers in this r

UVa 621 Secret Research (water ver.)

621 - Secret Research Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=562 At a certain laboratory results of secret research are thoroughly encrypted. A r

UVa 10079 Pizza Cutting (water ver.)

10079 - Pizza Cutting Time limit: 8.333 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1020 When someone calls Ivan lazy, he claims that it is his intelligence that helps

UVa 12250 Language Detection (water ver.)

12250 - Language Detection Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3402 English, Spanish, German, French, Italian and Russian are the 6 most promi