找到其位数乘以给定数字的最小数字 [英] Find the smallest number whose digits multiply to a given number

查看:96
本文介绍了找到其位数乘以给定数字的最小数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(初学者在这里)



我的任务说我必须找到数字乘以给定数字的最小数字。如果这样的数字不能输出-1



示例:



(beginner here)

My task says I have to find the smallest number whose digits multiply to a given number. If such number cannot be made output "-1"

Example:

10 = 25       2*5=10<br />
13 = -1  <br />
5 = 5     <br />
100 = 455     4*5*5 = 100



输入是单个数字n;


input is a single number n;

0 <= n <= 10^9





我的代码似乎工作正常,但每当我尝试将其上传到竞赛网站时,我的代码都没有通过所有测试用例。所有的测试用例都是隐藏的,所以我不知道我的代码失败的具体测试用例。



所以我想寻求帮助,我想知道我怎么能找到具体的测试用例甚至更好地帮我找到测试用例。



我的代码基于这些网站的代码:



最小数字k使得数字的乘积k等于n - GeeksforGeeks [ ^ ]



找到其位数乘以给定数字n的最小数字 - GeeksforGeeks [ ^ ]



我的C颂歌:





My code seems to work fine, but whenever I try to upload it to a contest website my code doesn't pass all the test cases. All test cases are hidden so I don't know the specific test case my code fails.

So I wanted to ask for help, I want to know how can I find that specific test case or even better help me find that test case.

My code is based on code from these websites:

Smallest number k such that the product of digits of k is equal to n - GeeksforGeeks[^]

Find the smallest number whose digits multiply to a given number n - GeeksforGeeks[^]

My code:

#include <iostream>
#include <stack>
using namespace std;

    // function to find smallest number k such that
    // the product of digits of k is equal to n
    long long int smallestNumber(long long int n)
    {
        // if 'n' is a single digit number, then
        // it is the required number
        if (n >= 0 && n <= 9)
            return n;

        // stack the store the the digits
        stack<long long int> digits;

        // repeatedly divide 'n' by the numbers 
        // from 9 to 2 until all the numbers are 
        // used or 'n' > 1
        for (long long int i = 9; i >= 2 && n > 1; i--)
        {
            while (n % i == 0)
            {
                // save the digit 'i' that divides 'n'
                // onto the stack
                digits.push(i);
                n = n / i;
            }
        }

        // if true, then no number 'k' can be formed 
        if (n != 1)
            return -1;

        // pop digits from the stack 'digits'
        // and add them to 'k'
        long long int k = 0;
        while (!digits.empty())
        {
            k = k * 10 + digits.top();
            digits.pop();
        }

        // required smallest number
        return k;
    }

    // Driver program to test above
    int main()
    {
        long long int n;//number i want to convert
        cin >> n;
        cout << smallestNumber(n);
        return 0;
    }



谢谢。



我尝试过:



我尝试过从1到1000000的数字,它们似乎都运行良好


Thank you.

What I have tried:

I tried numbers from 1 to 1000000 and they all seem to work fine

推荐答案

引用:

我的代码似乎工作正常,但每当我尝试将其上传到竞赛网站时,我的代码都没有通过所有测试用例。

My code seems to work fine, but whenever I try to upload it to a contest website my code doesn't pass all the test cases.



因为你的代码错了!

如果n = 12,当最佳答案是26时你的答案是223。


你在哪里尝试上传你的代码?


Because your code is wrong !
For n=12, your answer is 223 when the best answer is 26.

Where do you try to upload your code ?

引用:

所以我想问一下求助,我想知道如何才能找到具体的测试用例,甚至更好地帮助我找到测试用例。

So I wanted to ask for help, I want to know how can I find that specific test case or even better help me find that test case.



简答:你没有。

测试用例是故意保密的。

这样就可以避免提交只包含特定测试用例的答案列表的程序,以及没有解决问题的真正算法。


Short answer: you don't.
Test cases are kept secret on purpose.
This avoid submissions of program that just contain a list of answers for the specific test cases, and no real algorithm that solve the problem.


这篇关于找到其位数乘以给定数字的最小数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆