uva 993 Product of digits

原题:
For a given non-negative integer number N, find the minimal natural Q such that the product of all
digits of Q is equal N.
Input
The first line of input contains one positive integer number, which is the number of data sets. Each
subsequent line contains one data set which consists of one non-negative integer number N (0 ≤ N ≤10^9).
Output
For each data set, write one line containing the corresponding natural number Q or ‘-1’ if Q does not
exist.
Sample Input
3
1
10
123456789
Sample Output
1
25
-1
大意:
给你一个数,让你找一个最小的自然数Q,使得Q中的每一位数乘积就是这个数。

#include <bits/stdc++.h>

using namespace std;
//fstream in,out;
int Count[10];
vector<int> ans;
bool isPrime(int a)
{
    for(int i=2;i<=sqrt(a);++i)
        if(a%i==0)
        return false;
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    int n,Case;
    cin>>Case;
    while(Case--)
    {
        int flag2=0;
        ans.clear();
        cin>>n;
        memset(Count,0,sizeof(Count));
        if(n<10)
        {
            cout<<n<<endl;
            continue;
        }
        if(isPrime(n))
        {
            cout<<-1<<endl;
            continue;
        }
        while(n>0)
        {
            int flag=0;
            for(int i=9;i>=2;--i)
            {
                if(n%i==0)
                {
                    n/=i;
                    ans.push_back(i);
                    flag=1;
                    break;
                }
            }
            if(n==1)
                break;
            if(!flag)
            {
                flag2=1;
                cout<<-1<<endl;
                break;
            }
        }
        if(flag2)
            continue;
        sort(ans.begin(),ans.end());
        for(auto x:ans)
            cout<<x;
        cout<<endl;
    }
    return 0;
}

解答:
感觉这一部分的题目难度和cf上的a题和b题难度差不多,比较简单。
先判定这个数是否小于10,如果小于10那么直接输出。如果大于10判断是否是素数,如果是素数,输出-1。
如果不是素数,那么从9到2不断的去除,并记录被除数,如果出现除不开的情况输出-1。最后把记录的被除数排序输出即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值