Uva-993 Product of digits(简单贪心)

来源:互联网 发布:智能网络摄像机哪个好 编辑:程序博客网 时间:2024/05/01 21:07
题目大意:
能否找到一个自然数q,使它每位上的数字的积,等于n,如果q存在,就输出q,不存在就输出-1

解析:这是一道简单的贪心题,从9到2,对n进行分解,然后把因数存在一个数组中,如过最后n > 1就输出-1,否则反向输出那个数组。

#include <cstdio>#include <cstring>using namespace std;const int N = 1000;int num[N];int main() {int t,n;scanf("%d",&t);while(t--) {scanf("%d",&n);if(n == 1) {printf("1\n");continue;}int cnt = 0;for(int i = 9; i >= 2; i--) {while(n % i == 0) {num[cnt++] = i;n /= i;}}if(n > 1) {printf("-1\n");}else {for(int i = cnt-1; i >= 0; i--) {printf("%d",num[i]);}printf("\n");}}return 0;}

0 0
原创粉丝点击