URAL 1014

本文介绍了一道算法题目,要求找到最小的正整数Q,使得其各位数字的乘积等于给定的N。文章提供了详细的解题思路及代码实现。

题目:

1014. Product of Digits

Time limit: 1.0 second Memory limit: 64 MB

Your task is to find the minimal positive integer number Q so that the product of digits of Q is exactly equal to N.

Input
The input contains the single integer number N (0 ≤ N ≤ 10e9).

Output
Your program should print to the output the only number Q. If such a number does not exist print −1.

Sample
input
10

output
25

题意:
输入一个数n,寻找一个最小的数,要求这个最小的数的各个位上的数的乘积等于n,如果存在则输出这个数,不存在则输出-1.

思路:
很简单,将这n这个数分解,尽量分解成1~9中较大的数,然后将得到的数进行非降序排列,输出即可。
我直接写个递归,一个全局数组来记录分解的数,然后排序输出。

坑点:
Your task is to find the minimal positive integer number,我直接挂在这里了,最小的正整数,0要输出10,坑爹呀,最后几分钟写出了这题,结果差了这个条件,一直WA

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;


bool ok;
const int maxn = 100000;
int arr[maxn];
int cnt;


void dfs(int a){

    if(0<a && a<10) {arr[cnt++] = a; return;}
    else{
        for(int i=9; i>=2; i--){
            if(a%i==0){
                arr[cnt++] = i;
                dfs(a/i);
                return;
            }
        }
    }
    ok = true;
}

int main(){
    int n;
    while(cin>>n){
        if(n==0) {cout<<10<<endl; continue;}
        memset(arr, 0x3f, sizeof(arr));
        cnt = 0;
        ok = false;
        dfs(n);
        if(ok) {cout<<-1<<endl; continue;}
        sort(arr, arr+cnt);
        for(int i=0; i<cnt; i++)
            cout<<arr[i];
        cout<<endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值