SCU - 4576 数位之积

给你一个数字 ,你的任务是找一个最小的正整数 ,使得它的数位之积等于

,如果不存在,请输出-1.

Input

第1行为输入组数

第2—T+1行为数字

Output

每组样例输出一行。

Sample Input

3

1

16

11

Sample Output

1

28

-1

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
int a[100000],p=1;
bool cmp(int c,int d)
{
     
    return c<d;
}
int ss(int x,int pos)
{
     
    if(x<10&&x>=1){
     a[pos]=x;p=pos;return 1;}///一位数
    for(int i=9;i>=2;i--)///保证最小
    {
     
        if(x%i==0)
        {
     
            if(ss(x/i,pos+1)){
     a[pos]=i;return 1;}
        }
    }
    return 0;
}
int main() {
     
	int t,b;
	cin>>t;
	for(int i=1;i<=t;i++)
    {
     
       cin>>b;
       if(b==0)cout<<"10"<<endl;///坑点,注意题中说的是正整数!!!所以不是0
       else if(!ss(b,1))cout<<"-1"<<endl;
       else
       {
     
           sort(a+1,a+1+p,cmp);///从小到大排得最小数
           for(int k=1;k<=p;k++)cout<<a[k];
           cout<<endl;
       }
    }
	return 0;
}

你可能感兴趣的:(SCU - 4576 数位之积)