牛牛想對一個數做若干次變換,直到這個數只剩下一位數字。
變換的規則是:將這個數變成 所有位數上的數字的乘積。比如285經過一次變換后轉化成2*8*5=80.
問題是,要做多少次變換,使得這個數變成個位數。
輸入描述:
輸入一個整數。小於等於2,000,000,000。
輸出描述:
輸出一個整數,表示變換次數。
輸入例子:
285
輸出例子:
2
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 int n; 6 scanf("%d",&n); 7 if(n==0)//n是0,輸出 0次 8 printf("0"); 9 else 10 { 11 int result = 1; 12 int count = 0; 13 14 while(n/10)//n是個位數,輸出0次 15 { 16 while(n) 17 { 18 result *= n%10; 19 n = n/10; 20 } 21 printf("%d\n",result);//輸出每個數字相乘的積,作為新的n,本題中不需要此行的輸出 22 count++; 23 n = result; 24 result = 1; 25 } 26 27 printf("%d",count); 28 } 29 return 0; 30 }