vivo2020 spring school recruit - the product of the number of bits

Title Description

The product is now given any positive integer n, and the output look for the smallest positive integer m (m> 9), such that the m individual bits (bits, ten, one hundred ... ...) is equal to n, if -1 output exists.

My problem solution

Violence Act

The most original idea is traversed, since this number must not be less than n, then n from the start accumulation, and then converted into an array of characters, computes the product, determining
the silly method, and it is difficult termination condition is provided.

    public int solution (int n) {
        int a = n;
        while(true){
            char [] arr = Integer.toString(a).toCharArray();
            int m=1;
            for(char s:arr){
                m*=s-48;
            }
            if(m>6*n)return -1;
            else if (m==n) return a;
            a++;
        }
    }

Factorization method

The first number of complete factorization n = n1 * n2 ··· nx
if there is a factor of greater than 9, must not exist m, why?
We know that the factor is not necessarily a multiple of 2-9, then we must use the product of single-digit numbers that are not, it certainly does not exist m, direct -1
Thus we determined the factorization factor of 2 to 9 between
we use an array to store the number of times each factor appears. number ARR [i] i represents appears
to make the minimum m, then it must make large numbers low, as should be placed in the lower 9 bits like.
We from the upper to the lower second scanning array 9, to find all the possible composition of a large number of factors, for filling low.
The resulting number must be minimal.

    public int solution (int n) {
        int arr[] = new int[10]; //2~9用于存放因子个数
        for(int i=2;;i++){//因式分解,从2~9判断
            if(n==1)break;//完成
            if(i>9)return -1;//有大于9的因子,返回-1,结束
            if(n%i==0){  // 可以整除
                arr[i]++;//因子数量+1
                n = n/i; //除去因子
                i--;     //彻底分解,再次判断还能不能被i分解
            }
        }
        String res = ""; //用一个字符串存放结果
        for(int i=9;i>0;i--){ //从高位(9)开始扫描
            while(arr[i]>0){ //先看是否存在单个这样的数,如9
                res=i+res;
                arr[i]--;
            }
            //再看有没有多个数积是i的
            //由于不同的i 的因子组成不同,如8=2*2*2= 2*4,于是分情况讨论
            switch(i){
                case 9:
                    while(arr[3]>=2){
                        res="9"+res;
                        arr[3]-=2;
                    }
                    break;
                case 8:
                    while(arr[2]>=3){
                        res="8"+res;
                        arr[2]-=3;
                    }
                    while(arr[2]>0&&arr[4]>0){
                        res="8"+res;
                        arr[2]--;
                        arr[4]--;
                    }
                    break;
                case 6:
                    while(arr[2]>0&&arr[3]>0){
                        res="6"+res;
                        arr[2]--;
                        arr[3]--;
                    }
                    break;
                case 4:
                    while(arr[2]>=2){
                        res="4"+res;
                        arr[2]-=2;
                    }
                //7、5、3是素数,不能被非1因子分解
            }
        }
        return  Integer.valueOf(res);//返回结果
    }

Guess you like

Origin www.cnblogs.com/XT-xutao/p/12596299.html