UVA10922 2 the 9s【大数模除+进制】

xiaoxiao2021-02-28  57

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进制数一边进行模除取余数。

程序说明:(略)

题记:(略)

参考链接:(略)

 

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++) { 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; }

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-2630610.html

最新回复(0)