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 (0N
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。。要求出一个最小的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 (贪心 + 分解因子)的更多相关文章
-
UVa 993: Product of digits
这道题很简单.先将N用2,3,5,7(即10以内的素数)分解因数(需要先特殊判断N不为1),然后将可以合并的因数合并(如2*2合并成4,)这样求得的结果位数会减少,大小肯定会小一些.具体实现见代码. ...
-
D. Almost All Divisors(数学分解因子)
其实这题并不难啊,但是分解因子的细节一定要小心. \(比如样例48,2是因子说明24也是因子,也就是说假如x存在\) \(那么x一定是因子中的最小数乘上最大数\) \(那我们现在去验证x是否存在,先拿 ...
-
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 ...
-
(贪心5.2.6)URAL 1014 Product of Digits(利用数据有序化进行贪心选择)
/* * URAL_1014.cpp * * Created on: 2013年10月11日 * Author: Administrator */ #include <iostream> ...
-
UVa 11134 Fabled Rooks (贪心+问题分解)
题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...
-
Alternate Task UVA - 11728 (暴力。。分解质因子)
题意: 输入一个正整数S,(S <= 1000)求一个最大的正整数N,使得N的所有正因子之和为S. 解析: ..求1000以内的所有数的正因子和 ...输出.. #include <io ...
-
UVA 10791 Minimum Sum LCM(分解质因数)
最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...
-
UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
-
TOJ 4493 Remove Digits 贪心
4493: Remove Digits Description Given an N-digit number, you should remove K digits and make the new ...
随机推荐
-
[转载]JSON序列化与反序列化
转载:http://www.cnblogs.com/ejiyuan/archive/2010/04/09/1708084.html 方法一:引入System.Web.Script.Serializat ...
-
任E行M1端口比特率特征码
分辨率:800x480gps端口:com1比特率:4800设备特征码:01D1D008内存:128M
-
linux 输入子系统(3)----事件处理(input_handler层)
输入子系统主要设计input_dev.input_handler.input_handle.如下: [1]每个struct input_dev代表一个输入设备 struct input_dev { c ...
-
struts2讲义----建立一个struts2工程
建立一个Struts2 工程 Ø 1在MyEclipse中新建web工程 Ø 2在struts-2.2.1.1-all\struts-2.2.1.1解压struts2-blank.war( 最基础的示 ...
-
在线文档转换API word,excel,ppt等在线文件转pdf、png
在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...
-
简单实现服务器/客户端的c代码
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> ...
-
HBase之Table.put客户端流程(续)
上篇博文中已经谈到,有两个流程没有讲到.一个是MetaTableAccessor.getRegionLocations,另外一个是ConnectionImplementation.cacheLocat ...
-
1.7Oob 构造方法
1)构造方法 在创建对象后不用调用会自动执行,如无自定义构造会默认执行没有参数没有,且方法体中没有任何语句的, 2)构造方法在main入口开始后就执行
-
vue双向绑定(数据劫持+发布者-订阅者模式)
参考文献:https://www.cnblogs.com/libin-1/p/6893712.html 实现mvvm主要包含两个方面,数据变化更新视图,视图变化更新数据. 关键点在于data如何更新v ...
-
RHEL7安装图像化桌面
RHEL7安装图像化桌面 作者:Eric 微信:loveoracle11g 在安装系统的时候选择的是默认的Minimal Install RHEL7系统安装完成开机启动后发现没有图形化 Linux系统 ...