For a given non-negative integer number N, find the minimal natural Q such that the product of alldigits of Q is equal N.
Input
The first line of input contains one positive integer number, which is the number of data sets. Eachsubsequent line contains one data set which consists of one non-negative integer number N (0 ≤ N ≤109).
Output
For each data set, write one line containing the corresponding natural number Q or ‘-1’ if Q does notexist.
Sample Input
3
1
10
123456789
Sample Output
1
25
-1
问题简述:
给出一个数n,找出另外一个数,其各位数字的成绩等于n。
问题分析:对于数n,从9开始到2按照递减顺序试探能不能整除n。能整除n的话是一个可能的数字。
如果最后,n能够被这些数字整除,那么n除以各个数字之后就变为1。
各个数字还需要排序一下,小的在前,大的在后。
程序说明:
字符串连接运算时,稍微改变一下就不需要排序了!增加另外一个版本的程序。
题记:(略)
参考链接:(略)
AC的C++语言程序如下(无排序版本):
/* UVA993 Product of digits */
#include <iostream>
using namespace std;
int main()
{
int t, n;
cin >> t;
while(t--) {
cin >> n;
if(n <= 9)
cout << n << endl;
else {
string ans = "";
for(int i=9; i>=1; i--)
if(n % i == 0 && n > 1) {
n /= i;
ans = (char) (i + '0') + ans;
}
if(n > 1)
cout << -1 << endl;
else
cout << ans << endl;
}
}
return 0;
}
AC的C++语言程序如下:
/* UVA993 Product of digits */
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t, n;
cin >> t;
while(t--) {
cin >> n;
if(n <= 9)
cout << n << endl;
else {
string ans = "";
for(int i=9; i>=1; i--)
if(n % i == 0 && n > 1) {
n /= i;
ans += (char) (i + '0');
}
if(n > 1)
cout << -1 << endl;
else {
sort(ans.begin(), ans.end());
cout << ans << endl;
}
}
}
return 0;
}