算法找到整数,使得其数字产品是N-整数、算法、数字、产品

2023-09-11 04:24:05 作者:他的终场我会亲手写上

我的挑战之一,发现了这个问题。

问题是,我们将得到一个正整数N,我们需要找到最小号X的数字产品是N

我的做法是拳头找到素数与个位数 例如,对于10的倍数是2.5 100倍数是2,2,5,5

现在,我需要找到最小整数,所以我没有算2的3的无 如果有三个2的话,我更换三个双的一个8 如果有两个2的话,我会替代两个2的一个4 如果有两个3的话,我将取代两个3的一个9

你们能想出什么更好的算法中?

  INT getNumber(INT N){

    INT TEMP = N;
    int的一个[4] = {2,3,5,7};
    的std ::字符串str;
    做
    {
        对(INT I = 0; I&4; ++ⅰ)
        {
            INT VAL =气温%A [1];
            如果(VAL == 0)
            {
                个char [260];
                sprintf的(S,%D,A [1]);
                STR + = S;
                温度/ = A [1];
                打破;
            }

        }

    }而(温度大于1);
    焦炭中newstr [260];
    INT countof2 = 0,countof3 = 0;
    的for(int i = 0; I< str.length(); ++ I)
    {
        如果(海峡[I] =='2')
                ++ countof2;
        如果(海峡[I] =='3')
            ++ countof3;
    }
    布尔bFlag = FALSE;
    INT indexNew = 0;
        如果(countof2> = 3)
        {

            诠释计数= 0;
            对于(INT指数= 0;指数< str.length(); ++指数)
            {
                如果(STR [指数] =='2'和;&放大器;数3;)
                    ++计数;
                其他
                {
                    中newstr [indexNew] = STR [指数]
                    ++ indexNew;
                }
            }
            中newstr [indexNew] ='8';
            bFlag = TRUE;
        }
        否则如果(countof2&GT = 2)
        {
            中newstr [indexNew ++] ='4';
            诠释计数= 0;
            对于(INT指数= 0;指数< str.length(); ++指数)
            {
                如果(STR [指数] =='2'和;&安培;计数2)
                    ++计数;
                其他
                {
                    中newstr [indexNew] = STR [指数]
                    ++ indexNew;
                }
            }
            bFlag = TRUE;
        }
        否则如果(countof3&GT = 2)
        {
            诠释计数= 0;
            对于(INT指数= 0;指数< str.length(); ++指数)
            {
                如果(STR [指数] =='3'和;&安培;计数2)
                    ++计数;
                其他
                {
                    中newstr [indexNew] = STR [指数]
                    ++ indexNew;
                }
            }
            中newstr [indexNew] ='9';
            bFlag = TRUE;
        }

        中newstr [++ indexNew] ='\ 0';
        INT VAL = 0;
        如果(bFlag)
         VAL =的atoi(中newstr);
        其他
         VAL =与atoi(str.c_str());

    返回VAL;
}
 

解决方案

而不是寻找怪异的解决方案(2,2,2) - > 8,(3,3) - > 9等等,我只想去直正解(忘了素数,直接到所有数字):

 为(D = 9; D> 1; D--)
    同时(N%D == 0){
        N / = D;
        list.addDigit(四);
    }
如果(N = 1!)打印(无此号码);否则打印(反向(名单));
 

定义一个函数,它返回整数n从右边开始数的第k个数字

I found this problem in one of challenges .

Problem is that we are given an integer N and we need to find smallest number X whose digits product is N

My approach is to fist find prime numbers with single digit eg for 10 multiples are 2,5 for 100 multiples are 2,2,5,5

Now I need to find smallest integer so I count no of 2's and no of 3's If there are three 2's then I replace three 2's with one 8 If there are two 2's then I will replace two 2's with one 4 If there are two 3's then I will replace two 3's with one 9

Can you guys think of any better algo?

int getNumber(int n) {

    int temp = n;
    int a[4] ={2,3,5,7};
    std::string str;
    do
    {
        for(int i=0;i<4;++i)
        {
            int val = temp%a[i];
            if(val ==0)
            {
                char s[260];
                sprintf(s,"%d",a[i]);
                str += s;
                temp /=a[i];
                break;
            }

        }

    }while(temp>1);
    char newStr[260];
    int countof2=0,countof3=0;
    for(int i=0;i<str.length();++i)
    {
        if(str[i]=='2')
                ++countof2;
        if(str[i]=='3')
            ++countof3;
    }
    bool bFlag=false;
    int indexNew=0;
        if(countof2 >= 3)
        {

            int count =0;
            for(int index=0;index<str.length();++index)
            {
                if(str[index]=='2'&& count<3)
                    ++count;
                else
                {
                    newStr[indexNew]= str[index];
                    ++indexNew;
                }
            }
            newStr[indexNew]='8';
            bFlag=true;
        }
        else if(countof2 >=2)
        {
            newStr[indexNew++]='4';
            int count =0;
            for(int index=0;index<str.length();++index)
            {
                if(str[index]=='2'&& count<2)
                    ++count;
                else
                {
                    newStr[indexNew]= str[index];
                    ++indexNew;
                }
            }
            bFlag=true;
        }
        else if(countof3 >=2)
        {
            int count =0;
            for(int index=0;index<str.length();++index)
            {
                if(str[index]=='3'&& count<2)
                    ++count;
                else
                {
                    newStr[indexNew]= str[index];
                    ++indexNew;
                }
            }
            newStr[indexNew]='9';
            bFlag=true;
        }

        newStr[++indexNew]= '\0';
        int val=0;
        if(bFlag)
         val = atoi(newStr);
        else
         val = atoi(str.c_str());

    return val;
}

解决方案

Instead of looking for weird solutions with (2,2,2)->8, (3,3)->9 etc, I would just go with the straight forward solution (forget primes, go directly to all digits):

for (d = 9; d > 1; d--)
    while (n % d == 0) {
        n /= d;
        list.addDigit(d);
    }
if (n != 1) print("no such number"); else print(reverse(list));

 
精彩推荐
图片推荐