Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father’s magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit?
The first line contains the only integer n (0 ≤ n ≤ 10100000). It is guaranteed that n doesn’t contain any leading zeroes.
Print the number of times a number can be replaced by the sum of its digits until it only contains one digit.
Input 0 Output 0 Input 10 Output 1 Input 991 Output 3
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.
Fully high-precision operation, but due to array problems, run-time errors occur when too many test instances are available Do not rule out the existence of other errors, if found, please point the right in the comments area, or private chat me.
#include<stdio.h> #include<string.h> int main() { int x,s=0,i,b[100001]={0},m=0,j,f,k,t; char a; i=0; do { a=getchar(); x=a-48; b[i]=x; m++; i++; }while(a!='\n'); m--; while(m>1) { for(i=0;i<m/2;i++) { t=b[i]; b[i]=b[m-i-1]; b[m-i-1]=t; } for(j=1;j<m;j++) { b[0]+=b[j]; b[j]=0; for(i=0;i<=j;i++) { if(b[i]>=10) { b[i+1]+=(b[i]/10); b[i]%=10; } else break; } } for(i=m-1;i>0;i--) { if(b[i]==0) m--; else break; } s++; } printf("%d\n",s); return 0; }Because the largest number, by each add-on also does not exceed 900000, so read directly added, and then cycle. This string of code passes the judgment of Virtual Judge.
#include<stdio.h> int main() { int m=0,s=0,l=0; long long x=0,n; char a; do { x+=m; a=getchar(); m=a-48; l++; }while(a!='\n'); l--; if(x!=0&&l!=1) s++; while(x>=10) { n=x; x=0; while(n>=10) { x+=(n%10); n/=10; } x+=n; s++; } printf("%d\n",s); return 0; }