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;
	while ( cin >> n )
	while ( n -- ) {
		cin >> m;
		if ( m == 1 ) {
			printf("1\n");
			continue;
		}
		for ( int i = 9 ; i > 1 ; -- i )
			save[i] = 0;
		for ( int i = 9 ; i > 1 ; -- i )
			while ( m%i == 0 ) {
				save[i] ++;
				m /= i;
			}
		if ( m > 1 ) {
			printf("-1\n");
			continue;
		}else {
			for ( int i = 2 ; i < 10 ; ++ i )
				while ( save[i] -- > 0 )
					printf("%d",i);
			printf("\n");
		}
	}
	return 0;
}

UVa 993 - Product of digits

时间: 2024-08-07 02:09:45

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 (简单贪心)

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

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 10106 Product (大数相乘)

Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer. The Output For each input pair of lines the output line should c

UVA 10106 Product 高精度运算

J - Product Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Product  The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The

UVa 10106 Product 【大数相乘】WA

虽然是错的代码,但是还是想贴出来,最开始WA发现是没有考虑到乘积为0的情况,后来把a*0,0*a,a*0---0(若干个0),0--0(若干个0)*a都考虑进去了:可是还是WA,实在不懂先留在这儿.  Product  The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The input will consist of a set of pairs of lines. Each

10 URAL 1014 Product of Digits

找一个最小的正整数Q,Q的各个位置上的数字乘积等于N. 每一位只能是2-9,0和1没有用,那么能用尽量大的数就先用大的数,这样保证Q的位数最少, 从9枚举到2,如果W的因子只有9-2,那么有解,在把这些因子从小到大输出即可,否则无解. 注意要特判0和1,因为要求最小正整数,所以0的时候答案应该是10. #include<cstdio> int main() { int i,n,cnt,ans[40]; while(~scanf("%d",&n)) { if(n==0

uva 10106 Product

//简单大数相乘,用java很简单 import java.util.Scanner; import java.math.BigInteger; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); BigInteger a,b; while(in.hasNext()){ a = in.nextBigInteger(); b = in.nextBigInteg

URAL 1014 Product Of Digits

注意特判 0 1 分解因子 从9到2 1 import java.util.Scanner; 2 3 public class P1014 4 { 5 public static void main(String args[]) 6 { 7 try (Scanner cin = new Scanner(System.in)) 8 { 9 while (cin.hasNext()) 10 { 11 int n = cin.nextInt(); 12 StringBuilder builder =