首页 > 试题广场 >

变换次数

[编程题]变换次数
  • 热度指数:4500 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛想对一个数做若干次变换,直到这个数只剩下一位数字。
变换的规则是:将这个数变成 所有位数上的数字的乘积。比如285经过一次变换后转化成2*8*5=80.
问题是,要做多少次变换,使得这个数变成个位数。

输入描述:
输入一个整数。小于等于2,000,000,000。


输出描述:
输出一个整数,表示变换次数。
示例1

输入

285

输出

2
#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;
}

编辑于 2017-06-28 20:31:16 回复(5)
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;
}

发表于 2017-06-02 10:53:45 回复(0)
function num( number ) {
let count = 0;
while ( number > 10 ) {
number = (number + "").split("").reduce(function(x , y){
return x * y;
})
count++;
}
return count;
}
发表于 2017-05-25 10:15:23 回复(1)
#include <iostream>
#include <vector>
using namespace std;
int fun(int t);
int main()
{
int n;
int count = 0;
cin >> n;
while (n / 10)//n为个位数停止
{
n = fun(n);
count++;
}
cout << count;
return 0;
}
int fun(int t)
{
int mult = 1;
vector<int> vec;
while (t)
{
vec.push_back(t % 10);
t /= 10;
}
for (int i = 0; i < vec.size(); ++i)
{
mult *= vec[i];
}
return mult;
}
编辑于 2017-12-12 21:08:12 回复(0)
#include<stdio.h>
#include<inttypes.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int32_t main(){
    int64_t ans;
    int32_t count=0;
    cin>>ans;//输入一个数字
    while( ans > 9){
        int64_t temp=1;
        while(ans){
            temp*=ans%10;
            ans/=10;
        }
        ans=temp;
        count++;
    }
    cout<<count;
    return0;
}

发表于 2017-06-15 23:09:33 回复(0)
#include <iostream>
using namespace std;
int main()
{
	int a;
	int acou=0;
	cin>>a;
	while(a>9)
	{
		int b=a%10;
		while(a/10>0)
		{
			a/=10;
			b=b*(a%10);
		}
		acou++;
		a=b;
	}


	cout<<acou<<endl;
	//system("pause");
    return 0;
    
}

发表于 2017-05-23 21:01:03 回复(0)



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();
        }
    }

编辑于 2017-06-06 10:36:00 回复(0)
#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;
     
}

发表于 2017-05-20 18:18:17 回复(0)











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();

}


发表于 2017-05-22 21:34:44 回复(6)
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+"");
            }
        }
    }
}

编辑于 2022-09-18 19:20:19 回复(0)
num=int(input())
def change(num,count):
    str_num=str(num)
    sum_num=int(str_num[0])
    for i in range(1,len(str_num)):
        sum_num = sum_num * int(str_num[i])
    count += 1
    if sum_num < 10:
        return count
    else:
        return change(sum_num,count)
if num<10:
    print(0)
else:
    count=0
    print(change(num,count))
发表于 2020-04-23 15:59:49 回复(0)
from functools import reduce
def turn_int(n):
    return reduce(lambda x, y: x*y, [int(i) for i in str(n)])

if __name__ == '__main__':
    n = input("")
    n = int(n)
    count = 0
    while n > 10:
        count += 1
        n = turn_int(n)
    print(count)

发表于 2020-02-22 22:11:03 回复(0)
public class Main1 {

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        
        List<Integer> list=new ArrayList<Integer>();        //存放每一位上的数字
        
        int count=0;        //需要计算的次数
        int num=n;
        while(num>=10){
            while(n!=0){
                if(n%10==0){
                    System.out.println(++count);
                    return;
                }
                list.add(n%10);
                n/=10;
            }
            count++;
            num=1;
            for (Integer integer : list) {
                num*=integer;
            }
            n=num;
            list.clear();
        }
        
        System.out.println(count);
        scanner.close();
    }
}

发表于 2018-05-15 10:42:12 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] arg){
       
        Scanner s = new Scanner(System.in);
        Integer a = s.nextInt();
        System.out.print(test(a));
    }
    public static int test(Integer a){
        int count = 0;
        String str = a.toString();
        if(str.length()==1){
            return 0;
        }else{
            int x = 1;
            for(int i = 0;i<str.length();i++){
               x*=Integer.parseInt(str.substring(i,i+1));
            }
           return  test(x)+1;
        }
    }
}

发表于 2018-04-10 14:12:44 回复(0)
 
<?php
$str=(string)trim(fgets(STDIN));
$ee= str_split($str);
//$n = $ee[0];
$array= array_slice($ee,0);
$p= 0;
if(count($array)==1){
    echo$p;
    exit();
}else{
    do{
        $r= 1;
        $p+=1;
        for($i=0;$i<count($array);$i++){
            $r= $array[$i]*$r;
        }
        $str= (string)$r;  // 将int型转换成string
        $array= str_split($str);
 
    }while($r>=10);
    echo$p;
}
 
?>

发表于 2018-03-22 17:01:48 回复(0)
public class MysqlTest {
    private static int counts =0;
    public static void main(String[] args) throws Exception {
           changeNum(34622);
           System.out.println(counts);
    }
    
    public static void changeNum(int num){
        int sum =1;
        while((num/10)!=0){
            sum*= num % 10;
            num = num / 10;
        }
            sum*=num;
            counts++;
            if(sum/10!=0){
                changeNum(sum);
            }
    }
}

发表于 2017-10-31 19:07:35 回复(0)
package algorithmEx;

import java.util.Scanner;

public class TransformTimes {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String sn = sc.nextLine();
        int step = 0;
        if (!sn.contains("0")&&sn.length()>1) {
            int N = Integer.parseInt(sn);
            while (N > 9) {
                N = transform(N);
                step++;
            }
        }else {
            step=sn.length()>1?1:0;
        }
        System.out.println(step);
    }

    public static int transform(int N) {
        int newN = 1;
        while (N % 10 > 0) {
            newN *= N % 10;
            N /= 10;
        }
        return newN;
    }
}
 
发表于 2017-09-16 21:43:54 回复(0)
function exchange(x){  
 let i = 0;  
 while(x.toString().split('').length >1){  
   x = x.toString().split('').reduce((p,c) => p*c).toString();  
   i++;  
 }   
  return i;
 }

编辑于 2017-09-14 17:06:42 回复(0)

屁森的。。。

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))
发表于 2017-09-07 15:53:00 回复(0)
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();
	}

}
运行是对的,但不知道为什么通过不了,求解答

发表于 2017-08-31 20:03:05 回复(1)