24h購物| | PChome| 登入
2012-03-16 07:41:06| 人氣6,088| 回應0 | 上一篇 | 下一篇

[UVA] 10922 - 2 the 9s

推薦 0 收藏 0 轉貼0 訂閱站台

Problem C - 2 the 9s
Time Limit: 1 second


Introduction to the problem

A well-known trick to know if an integer N is a multiple of nine is to compute the sum S of its digits. If S is a multiple of nine, then so is N. This is a recursive test, and the depth of the recursion needed to obtain the answer on N is called the 9-degree of N.

Your job is, given a positive number N, determine if it is a multiple of nine and,if it is, its 9-degree.

Description of the input

The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits.

Description of the output

The output of the program shall indicate, for each input number, if it is a multiple of nine, and in case it is, the value of its nine-degree. See the sample output for an example of the expected formatting of the output.

Sample input:

999999999999999999999
9
9999999999999999999999999999998
0

Sample output

999999999999999999999 is a multiple of 9 and has 9-degree 3.
9 is a multiple of 9 and has 9-degree 1.
9999999999999999999999999999998 is not a multiple of 9.




#include <stdio.h>
#include <string.h>
int main() {
    char s[1001];
    while(scanf("%s", s) == 1) {
        if(strcmp(s, "0") == 0)
            break;
        int i, sum = 0;
        for(i = 0; s[i]; i++)
            sum += s[i]-'0';
        printf("%s is ", s);
        if(sum%9 != 0)
            puts("not a multiple of 9.");
        else {
            int degree = 1;
            while(sum >= 10) {
                sprintf(s, "%d", sum);
                sum = 0;
                for(i = 0; s[i]; i++)
                    sum += s[i]-'0';
                degree++;
            }
            printf("a multiple of 9 and has 9-degree %d.\n", degree);
        }
    }
    return 0;
}

台長: Morris
人氣(6,088) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][ConvexHull] 218 - Moth Eradication
此分類上一篇:[UVA] 10929 - You can say 11

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文