zoukankan      html  css  js  c++  java
  • leetcode -625-Minimum Factorization

    leetcode -625-Minimum Factorization

    625. Minimum Factorization 


    Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.

    If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.

    Example 1
    Input:

    48 
    Output:
    68

    Example 2
    Input:

    15
    Output:
    35

    题解: 

        题目的关键在于a的range, 注意当 a < 10 的情况。   

        同时,需要 vt 的size小于10,32-bit的integer的最高位数是10位,但是最高位的范围是 2,1......, 一定不存在1且最小只能是2,所以只能保留9位。

    class Solution {
    public:
        int smallestFactorization(int a) {
            if(a < 10){
                return a; 
            }
            vector<int> vt; 
            for(int i=9; i>=2; --i){
                if(a == 1){
                    break; 
                }
                while(a%i == 0){
                    vt.push_back(i); 
                    a = a/i; 
                }
            }
            if(a == 1 && vt.size() <= 9){
                int ans = 0; 
                for(int i=vt.size()-1; i>=0; --i){
                    ans = 10*ans + vt[i]; 
                }
                return ans; 
            }else{
                return 0; 
            }
        }
    };
    

      

  • 相关阅读:
    [COCI20142015#1] Kamp
    [CEOI2007]树的匹配Treasury
    [JLOI2016/SHOI2016]侦察守卫
    [POI2015]MOD
    [BJOI2017]机动训练
    [九省联考2018]一双木棋chess
    [清华集训2012]串珠子
    [POI2014]ZALFreight
    [SHOI2009]舞会
    [COCI2019]Mobitel
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7067100.html
Copyright © 2011-2022 走看看