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

被折叠的 条评论
为什么被折叠?



