牛牛想对一个数做若干次变换,直到这个数只剩下一位数字。
变换的规则是:将这个数变成 所有位数上的数字的乘积。比如285经过一次变换后转化成2*8*5=80.
问题是,要做多少次变换,使得这个数变成个位数。
输入描述:
输入一个整数。小于等于2,000,000,000。
输出描述:
输出一个整数,表示变换次数。
输入例子:
285
输出例子:
2
AC代码:
import java.util.Scanner;
public class Main {
public static String Mul(String str){
int res = 1;
char[] ch = str.toCharArray();
for (int i = 0 ; i < ch.length ; i++){
res *= (int)(ch[i] - '0');
}
return String.valueOf(res);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
StringBuffer str = new StringBuffer(in.next());
int res = 0;
if (str.length() == 1){
res = 0;
}else{
while(true){
String tmp = Mul(str.toString());
if (tmp.length() != 1){
res++;
str.delete(0, str.length());
str.append(tmp);
}else{
res++;
break;
}
}
}
System.out.println(res);
in.close();
}
}
复制
