牛牛想对一个数做若干次变换,直到这个数只剩下一位数字。
变换的规则是:将这个数变成 所有位数上的数字的乘积。比如285经过一次变换后转化成2*8*5=80.
问题是,要做多少次变换,使得这个数变成个位数。
#include<iostream>
using namespace std;
int main(){
int n, temp, count = 0;
cin >> n;
//当n不是一位数
while(n > 9){
//计算n的所有位数上的数字的乘积temp
temp = 1;
while(n){
//n%10为当前n末位上的数字
temp *= n % 10;
//移去末位
n /= 10;
}
//每次n变换为n的所有位数上的数字的乘积
n = temp;
//变换次数加一
count++;
}
cout << count << endl;
return 0;
}
var n = 0;
function k(a) {
var s = 1;
a.toString().split('').map((v, i) => {
s =s*v;
})
n++;
if (s.toString().split('').length !== 1) {
k(s);
}
return n;
}
using namespace std;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int count = 0;
while(num/10 != 0) {
ArrayList<Integer> a = new ArrayList<>();
while(num != 0) {
a.add(num%10);
num = num / 10;
}
int m = 1;
for(int i=0; i<a.size(); i++) {
m *= a.get(i);
}
num = m;
count ++;
}
System.out.println(count);
in.close();
}
}
#include<iostream>using namespace std;intmain(){longinput;longtemp;inttemp1;intcount=0;while(cin>>input){while(input>=10){temp=input;input = 1;count++;while(temp){temp1 = temp%10;//取出每位的值temp = temp/10;input = input*temp1;}}cout<<count<<endl;}return0;}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Integer a = s.nextInt();
// 开始循环
int count = 0;
while (a >= 10) {
int x = 1;
for (int i = 0; i < a.toString().length(); i++) {
x *= Integer.parseInt(a.toString().substring(i, i + 1));
}
a = x;
count++;
}
System.out.println(count);
s.close();
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.next();
if(str.length()==0 || str==null){
return;
}
//输入就是一位数字
if(str.length()==1){
System.out.println(0);
return;
}
//循环执行变换直至这个数只剩下一位数字
int count=0;
while(true){
count++;
int len = str.length();
long sum=1;
for(int i=0;i<len;i++){
int t = str.charAt(i)-'0';
if(t==0){
System.out.println(count);
return;
}else{
sum*=t;
}
}
if(sum<10){
System.out.println(count);
return;
}
str = new String(sum+"");
}
}
}
}
屁森的。。。
def count_times(num):
count = 0
while num >= 10:
temp_num = 1
str_list = list(str(num))
for i in str_list:
temp_num = temp_num * int(i)
num = temp_num
count += 1
return count
test_num = int(input())
print(count_times(test_num))
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()){
String str = input.next();
if(str.length() == 0 || str == null){
System.out.println("请输入正确的数字");
return;
}if(str.length() == 1){
System.out.println(0);
return;
}
int count = 0;
while(true){
count++;
int len = str.length();
long sum = 1;
for(int i=1;i<len;i++){
int t = str.charAt(i)-'0';
if(t==0){
System.out.println(count);
return;
}else{
sum *= t;
}
}if(sum<10){
System.out.println(count);
return;
}
str = new String(sum+"");
}
}
input.close();
}
}
运行是对的,但不知道为什么通过不了,求解答