UVA10922 2 the 9s【大数模除+进制】(老师的程序注释)

 

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.

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.

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.

 

 

题链接:UVA10922 2 the 9s

问题简述:(略)

问题分析:

  输入的整数很长,可能达到1000位,问能否被9整除。如果能则将各位数字相加,直到变成9,计算这个过程的步数并输出结果。

  这个问题包含一个大数模除求余数问题,可以一边将数字字符串转换为10进制数一边进行模除取余数。

程序说明:(略)

题记:(略)

参考链接:https://blog.csdn.net/tigerisland45/article/details/80174710

 

AC的C++语言程序如下:

/* UVA10922 2 the 9s */


#include <bits/stdc++.h>//几乎包含了所有的库函数 
 
using namespace std;
 
const int NINE = 9;
const int N = 1000;
char s[N + 1];
 
int main()
{
    while(~scanf("%s", s) && (s[0] != '0' || s[1] != '\0')) {
        int r = 0, sum = 0;
        for(int i = 0; s[i]; i++) {//s[i]是用串结束符结束循环,只要不是0就继续循环 
            sum += s[i] - '0';
 
            r *= 10;
            r += s[i] - '0';
            r %= NINE;
        }
 
        if(r)
            printf("%s is not a multiple of 9.\n", s);
        else {
            int dreep = 1;
            while(sum > 9) {
                dreep++;
 
                int t = sum;
                sum = 0;
                while(t) {
                    sum += t % 10;
                    t /= 10;
                }
            }
            printf("%s is a multiple of 9 and has 9-degree %d.\n", s, dreep);
        }
    }
 
    return 0;
}



/* 
原理:
比如 237 % 9
首先 2 % 9 = 2;
然后 (2*10 + 3) % 9 = 5;
然后(5*10 + 7) % 9 = 3;
所以 237 % 9 = 3;
*/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值