Java 欧拉工程 第三十一篇【考察英国货币面值的组合问题】
2014-09-04 12:09
176 查看
题目:
在英国,货币是由英镑£,便士p构成的。一共有八种钱币在流通:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) 和 £2 (200p).
要构造£2可以用如下方法:
1
£1 + 1
50p + 2
20p
+ 1
5p + 1
2p + 3
1p
允许使用任意数目的钱币,一共有多少种构造£2的方法?
原题:
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
解题思路:
这道题算是经典的背包问题了,我一开始想到的思路就是每个数字一个for,循环个7、8次遍历之和得到答案,不过细想觉得这样的算法实在太笨,百度一下,发现大多数的技术博客都用的这种本办法,么办法,我只好再google,不得不说老外在算法确实很有效率,用的是动态规划的思想,通过大题化小求最优解
首先由于参与求和的硬币只有8个,所以只有8种情况,使用硬币1;1,2;1,2,5;。。。。总计8种,
target 1 2 3 4 5
ways
1 2 2 3 3
在英国,货币是由英镑£,便士p构成的。一共有八种钱币在流通:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) 和 £2 (200p).
要构造£2可以用如下方法:
1
£1 + 1
50p + 2
20p
+ 1
5p + 1
2p + 3
1p
允许使用任意数目的钱币,一共有多少种构造£2的方法?
原题:
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
解题思路:
这道题算是经典的背包问题了,我一开始想到的思路就是每个数字一个for,循环个7、8次遍历之和得到答案,不过细想觉得这样的算法实在太笨,百度一下,发现大多数的技术博客都用的这种本办法,么办法,我只好再google,不得不说老外在算法确实很有效率,用的是动态规划的思想,通过大题化小求最优解
首先由于参与求和的硬币只有8个,所以只有8种情况,使用硬币1;1,2;1,2,5;。。。。总计8种,
target 1 2 3 4 5
ways
1 2 2 3 3
public class Launcher { public static void main(String[] args) { int coins[]={1,2,5,10,20,50,100,200}; int total=200; int[] matrix = new int[total+1]; matrix[0]=1; for(int i=0;i<coins.length;i++ ){ for(int j=coins[i];j<matrix.length;j++){ matrix[j]+=matrix[j-coins[i]]; } } System.out.println(matrix[200]); } }
相关文章推荐
- Java进阶之欧拉工程 第十五篇【网格路径问题】
- 还是字符组合问题,直接针对题目建模(java,算法探讨)
- Flex+Java组合处理某些日期(如公元1111年)的问题
- 在Android工程中运行Java程序问题
- java 排列组合问题汇总
- java-组合接口中的名字冲突问题
- Rational Rose 逆向工程(java) 常见问题二则
- android 工程开发问题Failed java.util.EmptyStackException
- 从n个数组中任意选取一个元素的所有组合的Java实现(组合问题)
- Java 非递归实现的排列组合中的平均分组问题
- 递归三兄弟——数的乘方、背包问题、组合的Java实现
- 一个.NET工程在调用Java方法的时候不能创建JVM的问题及其原因
- 一项浩大的JAVA编码工程(与unix兼容乱码问题)
- 解决Eclipse中Java工程间循环引用而报错的问题
- 在Android工程中运行Java程序问题
- linux 下部署Java工程编码问题
- Ubuntu 9.10下 Eclipse 新建Java工程按扭无反应问题
- 编译工程时报java:[1,0] illegal character: \65279问题排查与解决过程
- java实现从M个元素中取N个元素的所有组合(数学中的组合问题)