题目: 1.判断每个数是不是9的倍数,2.判断每个位数的总和是不是9的倍数, 并统计次数, 然后判断其总数的每个位数总和是否为9的倍数 并计数, 以此类推。(999 -> 27 -> 9 ---> degree = 2)
我們知道要怎麼確定一個整數是不是 9 的倍數-如果它每位數的總和是9的倍數,那它就是9的倍數。這種檢驗的方法其實是一種遞迴的方法,而且我們把這種方法遞迴的深度稱作 N 的 9-degree 。
你的工作就是,給你一個正整數N,判斷他是不是9的倍數,而且如果他是9的倍數你還需要判斷它的 9-degree。
Input
輸入含有多組測試資料。每組測試資料一列包含一個正數 N。
當 N=0 時代表輸入結束;輸入的數最大可以到1000位數。
Output
對於每一組測試資料,請輸出它是否是 9 的倍數及它的 9-degree。輸出格式請參考Sample Output。
Sample Input
999999999999999999999 9 9999999999999999999999999999998 837 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. 837 is a multiple of 9 and has 9-degree 2.
#include <stdio.h>
#include <string.h>
int main()
{
while(1)
{
char arr[1001]={0};
int i, sum=0, count=0, degree=0;
gets(arr);
if(arr[0]-'0'==0) break;
for(i=0; i<strlen(arr); i++)
sum+=arr[i]-'0';
while(sum%9==0)
{
if(sum==9)
{
degree++;
break;
}
count=0;
while(sum!=0)
{
count+=sum%10;
sum/=10;
}
sum=count;
degree++;
}
printf("%s ",arr);
if(sum%9==0) printf("is a multiple of 9 and has 9-degree %d.\n",degree);
else printf("is not a multiple of 9.\n");
}
}