POJ2325
来源:互联网 发布:js点击事件 编辑:程序博客网 时间:2024/06/06 00:08
Problem : Persistent Numbers
Description : 给你一个大整数A,要你求一个最小的整数,使得你求的整数的各个位置上的数字的乘积等于给出的A。
Solution : 大整数+贪心+排序。既然要求的数的各位上的数字乘积要等于A,那么我们就一位一位来剥离这个数字,所以这里要用到出发,但是,题目要求最小,如何才能最小,首先得位数最小,其次字典序最小。我一开始想在模拟的过程中把这两者一起考虑,但是总是不能成功,当我考虑字典序最小时,位数又大了,考虑位数最小时,字典序大了。当我考虑位数最小时,发现得出的答案只要从小到大排个序就是正确答案了。这时候我才发现一个数字可以由很多个小于10质数组成,例如
Code(JAVA) :
import java.math.BigInteger;import java.util.Arrays;import java.util.Scanner;public class Main { public Main() { Scanner cin = new Scanner(System.in); while (cin.hasNext()) { BigInteger N = cin.nextBigInteger(); if (N.equals(BigInteger.valueOf(-1))) break; if (N.toString().length() == 1) { System.out.println("1" + N.intValue()); continue; } StringBuffer ans = new StringBuffer(""); BigInteger now = N; boolean f = true; for (;;) { if (now.toString().length() == 1) { ans.append(now.intValue()); break; } boolean flag = false; int pre = 0; for (int i = 9; i >= 2; i--) if (now.mod(BigInteger.valueOf(i)).equals(BigInteger.ZERO)) { pre = i; flag = true; break; } if (!flag) { f = false; break; } ans.append(pre); now = now.divide(BigInteger.valueOf(pre)); } if (f) { char[] a = ans.toString().toCharArray(); Arrays.sort(a); System.out.println(a); } else System.out.println("There is no such number."); } } public static void main(String[] args) { new Main(); }}
0 0
- POJ2325
- poj2325 大数除法+贪心
- POJ2325 Persistent Numbers(大数+贪心)
- poj2325(贪心+大整数除法)
- 【POJ2325】Persistent Numbers 贪心+高精度/低精度
- 【flex】无论是php java后台服务器在域名的根目录添加 crossdomian.xml文件实现flash 安全策略,从而实现通讯
- ZooKeeper安装配置
- Zookeeper命令操作
- SDUT数据结构实验之二叉树一:树的同构
- Java中的对象和类
- POJ2325
- Activity的使用
- Java——二分折半查找法
- POJ3616
- ios某一个屏幕支持旋转,其他不支持的方法
- MySQL Administrator
- Uncaught TypeError: Cannot read property 'width' of undefined
- 阅读Sofia-SIP源码 - su模块 - su_default_log.c/su_global_log.c
- Swift中网络单例类封装