ural 1014. Product of Digits
1014. Product of Digits
Time Limit: 1.0 second
Memory Limit: 64 MB
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 ≤ 109).
Output
Your program should print to the output the only number Q. If such a number does not exist print −1.
Sample
| input | output |
|---|---|
10 |
25 |
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int main(){
long long n;
cin >> n;
if(n == 0){cout<<10<<endl;return 0;}
if(n == 1){cout<<1<<endl;return 0;}
vector<int> product;
bool flag = false;
while(n!=1){
int i;
for(i = 9;i>1; i -- ){
if(n%i == 0) {product.push_back(i);n /=i; ;break;}
}
if( i== 1) {
if(n < 10) product.push_back(n);
else flag = true;
break;
}
}
if(flag) {cout<<-1<<endl;return 0;}
long long ans = 0;
for(int i = product.size()-1 ; i >= 0 ; i-- ){
ans = ans*10+product[i];
}
cout<<ans<<endl;
return 0;
}

浙公网安备 33010602011771号