uva 10922 - 2 the 9s

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;
#define maxn 1005
#define inf 0x7ffffff
pair <int ,int > p;
char arr[maxn];
int n;
int main()
{
    while(gets(arr)){
        int len = strlen(arr);
        if(strcmp("0",arr) == 0){
            break;
        }
        int sum = 0;
        for(int i = 0; i < len;i++){
            sum += arr[i] - '0';
        }
        if(sum == 0){
            break;
        }
        bool flag = false;
        if(sum % 9 != 0){
            flag = true;
        }
        int cnt = 0;
        while(++cnt && sum > 9){
           int tmp = sum;
           int sub = 0;
           while(tmp){
                sub += tmp % 10;
                tmp /= 10;
           }
           if(sub % 9 != 0){
            flag = true;
            break;
           }
           sum = sub;
        }
        if(flag){
            printf("%s is not a multiple of 9.\n",arr);
        }else{
            printf("%s is a multiple of 9 and has 9-degree %d.\n",arr,cnt);
        }
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值