Product of digits 

For a given non-negative integer number N , find the minimal natural Q such that the product of all digits of Q is equal N .

Input

The first 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 (0N109) .

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。。要求出一个最小的Q。。使得Q的每位的乘积等于N。。

思路:贪心。。先把N分解成质因数保存起来。。。然后拿这些质因数去组合。。由于Q要最小。所以先使得位数尽量小。再使得每个位数上的数字尽量小。。就是答案了。。因此我们在组合的时候。要尽量往大了去组合。由于只能组合成一位数。所以从9开始组合。9需要2个3,8需要3个2,6需要2和3。4需要2个2组合。。按9, 8, 6, 4的顺序组合。。最后剩下的数字一定最少。然后在组合后每个数字的个数,按从小到大输出即可。。

..然后看了别人的题解。。。才发现。。其实直接从9开始分解因子就可以了。。我还先分解成质因子了在去组合- - 2B了。没考虑清楚就直接开始写的后果。。还好这题写的还不算太长

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
using namespace std; int t, n;
int num[10]; int solve() {//质因数去组合。。
num[9] += num[3] / 2;
num[3] %= 2;
num[8] += num[2] / 3;
num[2] %= 3;
int sb = min(num[2], num[3]);
num[6] += sb;
num[2] -= sb;
num[3] -= sb;
num[4] += num[2] / 2;
num[2] %= 2;
} int main() {
scanf("%d", &t);
while (t --) {
scanf("%d", &n);
memset(num, 0, sizeof(num));
if (n == 0 || n == 1) {//0和1单独出来考虑。
printf("%d\n", n);
continue;
}
for (int i = 2; i <= 7; i ++) {//分解质因数枚举到7就可以了。因为一个位数上的数字必须是一位数。
while (n % i == 0 && n != i) {
num[i] ++;
n /= i;
}
}
if (n > 9)
printf("-1\n");
else {
num[n] ++;
solve();
for (int i = 2; i <= 9; i ++) {//按从小到大输出。
while (num[i]) {
printf("%d", i);
num[i] --;
}
}
printf("\n");
}
}
return 0;
}

uva 993 Product of digits (贪心 + 分解因子)的更多相关文章

  1. UVa 993: Product of digits

    这道题很简单.先将N用2,3,5,7(即10以内的素数)分解因数(需要先特殊判断N不为1),然后将可以合并的因数合并(如2*2合并成4,)这样求得的结果位数会减少,大小肯定会小一些.具体实现见代码. ...

  2. D. Almost All Divisors(数学分解因子)

    其实这题并不难啊,但是分解因子的细节一定要小心. \(比如样例48,2是因子说明24也是因子,也就是说假如x存在\) \(那么x一定是因子中的最小数乘上最大数\) \(那我们现在去验证x是否存在,先拿 ...

  3. UVA 10780 Again Prime? No Time. 分解质因子

    The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...

  4. (贪心5.2.6)URAL 1014 Product of Digits(利用数据有序化进行贪心选择)

    /* * URAL_1014.cpp * * Created on: 2013年10月11日 * Author: Administrator */ #include <iostream> ...

  5. UVa 11134 Fabled Rooks (贪心+问题分解)

    题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...

  6. Alternate Task UVA - 11728 (暴力。。分解质因子)

    题意: 输入一个正整数S,(S  <= 1000)求一个最大的正整数N,使得N的所有正因子之和为S. 解析: ..求1000以内的所有数的正因子和 ...输出.. #include <io ...

  7. UVA 10791 Minimum Sum LCM(分解质因数)

    最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...

  8. UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)

    题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...

  9. TOJ 4493 Remove Digits 贪心

    4493: Remove Digits Description Given an N-digit number, you should remove K digits and make the new ...

随机推荐

  1. oracle的decimal和number的对比

    Oracle只是在语法上支持decimal类型,但是在底层实际上它就是number类型,支持decimal类型是为了能把数据从Oracle数据库移到其他数据库中(如DB2等). 因为decimal在O ...

  2. 3dmax详细讲解全套攻略在线视频教程

    课程目录 试学课 课时11MAX2012学习软件指导 试学课 课时22MAX2012界面介绍 试学课 课时33MAX2012工作准备设置 试学课 课时44长方体的创建 试学课 课时55圆锥体的创建 试 ...

  3. ASP.NET 将DataTable解析成JSON简介

    这里解析json使用的是Newtonsoft.Json.dll程序集.下面请看code: using System; using System.Collections.Generic; using S ...

  4. CentOS 6,7最小化安装后再安装图形界面

    CentOS 6.2最小化安装后再安装图形界面 在安装CentOS 6.2时发现它没有提示我要怎么安装,而是“自作主张”地给我选择了最小化安装,结果装完之后只有终端界面,因为有时候不得不用图形界面,所 ...

  5. 求小于等于n的所有素数

    题目:要求输出所有小于等于n的素数(n>=2,且为正整数) 要求:1.每行输出10个素数 2.尽可能采用较优算法 #include<iostream> #include<cma ...

  6. ubuntu14.04 中文输入法无法使用

    说下我的解决方法吧,我是忘了在All Settings -> Text Entry 的 Input sources to use中添加Chinese(Pinyin)了,添加后就好了. from: ...

  7. oracle 行转列 分析函数

    oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...

  8. [译] ASP.NET 生命周期 – ASP.NET 上下文对象(六)

    使用 HttpApplication 对象 ASP.NET 框架中的许多类都提供了许多很方便的属性可以直接映射到 HttpContext 类中定义的属性.这种交叠有一个很好的例子就是 HttpAppl ...

  9. 【BZOJ1468】Tree

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

  10. 如何修改SVN中的用户名和密码

    删除掉C:\Users\Administrator\Application Data\Subversion\auth\svn.simple文件夹下的文件即可.再次访问时,会弹出要求输入用户名和密码. ...