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.
问题简述:(略)
问题分析:
输入的整数很长,可能达到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;
*/