描述
找到一个最小的正整数Q,使Q的各位数的乘积等于N。
输入
多组测试数据。数据以EOF结尾。
输入一个整数N(0≤N≤400)。
输出
输出Q,如果Q不存在则输出−1。
样例输入
10
5
样例输出
25
5
思路:
1.判断是否为0,为0直接输出10。
2.不为0,判断是否是个位数,若为个位数,直接输出本身。
3.不为个位数
倒序统计n从9到2的因子个数。(保证位数最少,同时值也为最小)
4.从小到大依次输出因子即可。
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int res[10];
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
memset(res, 0, sizeof(res));
if(n == 0)
{printf("10\n"); continue;} //易错
else if(n < 10)
{printf("%d\n", n); continue;}
else
{
int tmp = n;
for(int i = 9; i >= 2; ) // 统计2-9的因子个数(倒序是关键~)
{
if(tmp % i == 0)
{
res[i]++;
tmp /= i;
}
else
i--;
}
if(tmp > 10) {printf("-1\n"); continue;}
for(int i = 2; i <= 9; ++i) //从小到大输出每个因子即可
for(int j = 1; j <= res[i]; ++j)
printf("%d", i);
printf("\n");
}
}
return 0;
}