題目意思:讀取一數字,此數字最大有1000位。計算該數字是否為九的倍數?如是,再計算其階層數。
※判斷是否為九的倍數:所有位數相加 ÷ 9=0,即為九的倍數。
※計算階層數:所有位數相加後得出的第一個總和為一階,依此類推直到該數字無法再計算總和。
Example 1:
999 => 27(9+9+9, 1階) => 9(2+7, 2階)
Example 2:
99999 99999 99999 99999 9 => 189(9+9+...+9, 1階) => 18(1+8+9, 2階) => 9(1+8, 3階)
#include <cstdio>
using namespace std; int main()
{
char str[];
while(scanf("%s", str) && str[] != '')
{
int degree = , sum = ;
for(int i = ; str[i]; i++)
sum += str[i] - '';
degree ++;
if( !(sum % ))
{
while(sum > )
{ degree ++;
int tempsum = , temp = sum;
while(temp)
{
tempsum += temp % ;
temp /= ;
}
sum = tempsum;
}
printf("%s is a multiple of 9 and has 9-degree %d.\n", str, degree);
}else printf("%s is not a multiple of 9.\n", str);
}
return ;
}