牛牛想对一个数做若干次变换,直到这个数只剩下一位数字。
变换的规则是:将这个数变成 所有位数上的数字的乘积。比如285经过一次变换后转化成2*8*5=80.
问题是,要做多少次变换,使得这个数变成个位数。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int transform(int x) {
LL ans = 1;
while(x) {
ans *= x % 10;
x /= 10;
}
return ans;
}
int main() {
int x;
cin >> x;
int step = 0;
while(x >= 10) {
x = transform(x);
step++;
}
cout << step << endl;
return 0;
} import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count=0;
while(n>10){
String s = n+"";
if(s.contains("0")){
count++;
break;
}else{
n =1;
for(int i=0;i<s.length();i++){
n = n*(s.charAt(i)-'0');
}
count++;
}
}
System.out.println(count);
}
}