內容
給你一個大於等於 0 的整數 N,請你你找到最小的自然數 Q ,使得在 Q 中所有數字(digit)的乘積等於 N 。
例如:N=10, 可以找到Q=25,因為 2*5=10
輸入說明
輸入的第一列有一個整數代表共有多少組測試資料。
每組測試資料一列有1個整數 N(0 <= N <= 1000000000)
請參考Sample Input。
輸出說明
每組測試資料輸出一列,輸出自然數 Q ,如果 Q 不存在,請輸出 -1。
範例輸入 #1
5 1 10 123456789 216 26
範例輸出 #1
1 25 -1 389 -1
C++:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t,n;
cin>>t;
while(t--){
while(cin>>n){
if(n==1){
cout<<1<<"\n";
continue;
}
vector<int> v;
//cout<<"222";
for(int i=9;i>1;i--){
if(n%i==0){
v.push_back(i);
//cout<<"111";
n/=i;
}
}
if(n!=1){
cout<<-1<<"\n";
continue;
}
reverse(v.begin(),v.end());
for(auto i:v)cout<<i;
cout<<"\n";
}
}
return 0;
}
python:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t,n;
cin>>t;
while(t--){
while(cin>>n){
if(n==1){
cout<<1<<"\n";
continue;
}
vector<int> v;
//cout<<"222";
for(int i=9;i>1;i--){
if(n%i==0){
v.push_back(i);
//cout<<"111";
n/=i;
}
}
if(n!=1){
cout<<-1<<"\n";
continue;
}
reverse(v.begin(),v.end());
for(auto i:v)cout<<i;
cout<<"\n";
}
}
return 0;
}
文章標籤
全站熱搜
