首页 > 试题广场 >

变换次数

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

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


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

输入

285

输出

2
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)
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  System.out.println("请输入一个整数:");
  int temp=sc.nextInt();
  int count=0;
  while(temp>=10){
   ArrayList<Integer> a=new ArrayList<>();
   while(temp>0){
    a.add(temp%10);
    temp/=10;
   }
   int sum=1;
   for(int i=0;i<a.size();i++){
    sum*=a.get(i);
   }
   System.out.println("第"+(count+1)+"次");
   System.out.println(sum);
   temp=sum;
   count++;
  }
  System.out.println("总共执行了"+count+"次");
  sc.close();
 }
}

编辑于 2017-08-12 10:54:31 回复(0)
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();

        if (i <= 2000000000) {
            System.out.println(fun(i)+"");
        }

    }

    /**输出需要变换的次数
     * @return  变换的次数
     */
    private static int fun(int input) {

        int result = 0;
        int length = getLength(input);
        System.out.println("length = "+length);
        if (length == 1) {
            result = 1;
        } else {
            result = function(input, length,1);
        }

        return result;
    }


    private static int function(int input, int length,int numTmp) {

        if (length == 1) {
            return numTmp;
        }

        //求出每一位数
        int tmp = (int) Math.pow(10,length-1);
        int[] tmps = new int[length];
        if (tmp != 0) {
            for(int i = 0; i < length ; i++) {
                tmps[i] = input / tmp;
                input = input % tmp;
                tmp = tmp / 10;
            }
        }

        int s = 1;
        for (int j = 0;j<tmps.length;j++) {
            s = s * tmps[j];
        }

        if (getLength(s) == 1) {
            return numTmp;
        } else {
            numTmp++;
            return function(s,getLength(s),numTmp);

        }

    }

    /**判断输入数字的位数
     * @param input
     * @return
     */
    private static int getLength(int input) {
        if (input == 0) {
            return 1;
        }
        int length = 0;

        while (input != 0) {
            input /= 10;
            length++;
        }
        return length;
    }




}

发表于 2017-06-17 16:39:10 回复(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)

热门推荐

通过挑战的用户