題目連結: https://zerojudge.tw/ShowProblem?problemid=d672
# 解題思路
先運用字串計算每個位數的和,之後就可以用整數配 while 的方式將次數算出囉~
# 程式碼
#include <bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
char x[10000]; | |
while(~scanf("%s",&x)){ | |
if(strcmp(x,"0")==0) break; | |
int y=1,sum=0,s=strlen(x); | |
for(int i=0;i<s;i++) sum+=x[i]-'0'; | |
while(!(sum%9)){ | |
if(sum==9) break; | |
int sum1=0; | |
while(sum){sum1+=sum%10,sum/=10;} | |
sum=sum1,y++; | |
} | |
if(sum%9) printf("%s is not a multiple of 9.",x); | |
else printf("%s is a multiple of 9 and has 9-degree %d.",x,y); | |
printf("\n"); | |
} | |
} |