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

993 Product of digits

For a given non-negative integer number N, ?nd the minimal natural Q such that the product of all

digits of Q is equal N.

Input

The ?rst line of input contains one positive integer number, which is the number of data sets. Each

subsequent 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 not

exist.

Sample Input

3

1

10

123456789

Sample Output

1

25

-1

思路:找N的9 ~ 2的因数(按降序找)的个数,输出按升序

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int T, N;
int a[10];

int main() {
	scanf("%d", &T);
	while(T--) {
		scanf("%d", &N);
		if(N == 1) {
			printf("1\n");
			continue;
		}
		memset(a, 0, sizeof(a));
		for(int i = 9; i >= 2; i--) {
			if(N == 1) break;
			while(N % i == 0) {
				a[i] ++;
				N /= i;
			}
		}
		if(N != 1) {
			printf("-1\n");
			continue;
		}
		for(int i = 2; i < 10; i++) {
			while(a[i] != 0) {
				printf("%d", i);
				a[i] --;
			}
		}
		printf("\n");
	}
	return 0;
}

时间: 2024-08-24 12:07:19

UVA - 993 - Product of digits (简单贪心)的相关文章

uva 993 Product of digits (分解因子)

uva 993 Product of digits 题目大意:给定一个数N,要求出一个数,使得这个数的每一位相乘会等于N,且这个数的值最小. 解题思路:简单的分解因子.要注意的地方有两点:1) 因子从9开始向2遍历,可以保证位数最小: 2)当N等于1是最好特殊处理. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std;

UVa 993 - Product of digits

题目:给你一个整数n,求一个数m,使得m的每个位数的乘积是n,求最小的m. 分析:贪心.直接从9到2枚举所有的因数,统计即可.如果还有大于9的素数这输出-1. 说明:今天Timus发邮件,说我的这个题目,在那边的解错了╮(╯▽╰)╭. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int save[10]; int main() { int n,m,now; w

993 - Product of digits

题目:993 - Product of digits 题目大意:将一个数通过因式分解变成另一个由因子组成的最小的数. 解题思路:因为要组成数,所以因子只需要从 2 - 9,又因为需要最小的数,所以因式分解从最大的因子开始,这样位数最少,最后输出从最小的因子输出,保证最小.1的话需要特判,因为所有的数都有因子1. 代码: #include <stdio.h> #include <string.h> const int N = 10; int c[N]; int n; bool fac

UVA 11292 Dragon of Loowater(简单贪心)

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

UVA - 10700 - Camel trading (简单贪心)

UVA - 10700 Camel trading Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem E - Camel trading Time Limit: 1 second Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formu

UVA - 108 - Maximum Sum (简单贪心)

UVA - 108 Maximum Sum Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension.

POJ 2393 Yogurt factory(简单贪心)

http://poj.org/problem?id=2393 题意:任务规定,一个酸奶制造厂,在n个星期内,分别要向外提供y[i]unit的酸奶.已知这个制造厂第i周制造每unit酸奶的费用为c[i],储存室储存每1unit酸奶1星期的费用为s.问要完成这个任务的最小费用是多少. . . . . . . . . . . . . . 思路:简单贪心.维护一个目前最优的代价minc = min(c, minc + s),然后求和. #include <iostream> using namespa

uva:10700 - Camel trading(贪心)

题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这样的表达式加上括号后能得到的最大值和最小值. 解题思路:因为这些数的都是正整数,所以可以用贪心.不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法.注意这里的数值要用long long 因为比表达式的值可能会超过int. 代码: #include <stdio.h> #include <string.h> cons

POJ 3069 Saruman&#39;s Army (简单贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5343   Accepted: 2733 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his forces, Saruman distributes se