题目:
输入:
输出:
示例 :
提示: In the first sample the number already is one-digit — Herald can’t cast a spell. The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once. The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit.
python👇【检验超时】
a=input()
b=list(a)
c=[]
s=0
i=1
while 1:
for j in range(len(b)):
s+=int(b[j])
if s==0:
print(0)
break
if s<9:
print(i)
break
else:
b=list(str(s))
c=[]
s=0
i+=1
C++👇
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include<set>
#include <algorithm>
using namespace std;
char s[100100];
int main()
{
int n,cnt,i,res;
scanf("%s",s);
cnt=0;
while (s[1])
{
cnt++;
res=0;
for(i=0;s[i];i++)
{
res+=s[i]-'0';
}
sprintf(s,"%d",res);
}
printf("%d\n",cnt);
return 0;
}