uva 993 Product of digits (贪心 + 分解因子)
| 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 (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。。要求出一个最小的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 ...
随机推荐
- Android 布局 中实现适应屏幕大小及组件滚动
要实现如图的布局: 这是在eclipse可视化窗口中的截图,但实际运行在Android设备上可能出现的问题有: (1):当编辑图1中的最后一个EditText时,输入法的编辑界面会把底部的Button ...
- php提取字符串中的数字
最近工作中写代码的时候需要在一串字符串中将所有的数字提取出来这么一个小功能,研究了一下发现方法还挺多,值得记录一下,于是对如何使用PHP将字符串中的数字提取出来的功能做了一个小总结,总结三种方法如下: ...
- (转载)总结一下SQL语句中引号(')、quotedstr()、('')、format()在SQL语句中的用法
总结一下SQL语句中引号(').quotedstr().('').format()在SQL语句中的用法 总结一下SQL语句中引号(').quotedstr().('').format()在SQL语句中 ...
- 在asp.net mvc中如何使用Grid++ Report (锐浪报表)
在asp.net mvc中如何使用Grid++ Report (锐浪报表) 在cshtml,razor中的处理方法 以官方的asp.net(csharp)中的第一个示例"1a.简单表格&qu ...
- 如何在eclipse中配置Selenium
1, Install python 33.(Python 27也可以) 2, Setup Selenium If you did not install Easy_install module, yo ...
- Flex读文本文件
布局: <s:Group id="> <s:Rect width="100%" height="100%"> <s:fi ...
- Oracle监听器—动态注册
注册就是将数据库作为一个服务注册到监听程序.客户端不需要知道数据库名和实例名,只需要知道该数据库对外提供的服务名就可以申请连接到数据库.这个服务名可能与实例名一样,也有可能不一样. 注册分: 1. 静 ...
- PHP - PDO 之 mysql 参数绑定
<?php /* pdo 学习 */ $dsn = 'mysql:host=localhost;dbname=cswl';//构建连接dsn $db = new pdo($dsn,'root', ...
- sybase convert 函数
1.从string到int的转换 convert(int,@string) select convert( int , '15') 2. 从int 到 decimal 的转换 convert(deci ...
- Google面试题
今天早上在Quora上看到的一个题目,很不错的!最直观的是枚举n^3,但稍微进步一点的观察是找出3个数,然后最大的减去最小的2倍的结果,然后就有了线性扫一遍就OK. Given three array ...