[ACM_NYOJ_270]Product of Digits
杰拉斯 | 时间:2012-04-20, Fri | 18,379 views编程算法
Product of Digits
Time Limit: 1.0 second
Memory Limit: 16 MB
Description
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
10 5
Sample Output
25 5
Source
陷阱啊陷阱。。。positive integer number即正整数,也就是说输入0应输出10,而非0。代码如下,寥寥草草写的没怎么优化过:
#include<iostream> using namespace std; int main() { int num; int a[100]; while(cin >> num){ int k = 0; if(num == 0){ cout << 10 << endl; continue; }else if(num < 10){ cout << num << endl; continue; } for(int i = 9; i >= 2; --i){ while(num){ if(num % i == 0){ a[k++] = i; num /= i; }else{ break; } } } if(num > 1){ cout << -1 << endl; }else{ for(int j = k - 1; j >= 0; --j){ cout << a[j]; } cout << endl; } } return 0; }
如需转载请注明出处:杰拉斯的博客
当前暂无评论 »