贪心算法实例(二):钱币找零问题
2017-06-17 09:39
337 查看
假设1元、2元、5元、10元、20元、50元、100元的纸币分别有c0, c1, c2, c3, c4, c5, c6张。现在要用这些钱来支付K元,至少要用多少张纸币?
用贪心算法的思想,很显然,每一步尽可能用面值大的纸币即可。在日常生活中我们自然而然也是这么做的。
在程序中已经事先将Value按照从小到大的顺序排好。
具体实现代码如下 所示:
/**
*@Title: LightMoney.java
*@Package greedyalgorithm
*@Description: TODO
*@author peidong
*@date 2017-5-16 上午9:13:12
*@version V1.0
*/
packagegreedyalgorithm;
/**
* @ClassName: LightMoney
* @Description: 钱币找零问题
* @date 2017-5-16 上午9:13:12
*
*/
publicclass LightMoney {
public static int N = 7;
public static int num = 0;
public static int[] count = {3, 0, 2, 1,0, 3, 5};
public static int[] value = {1, 2, 5, 10,20, 50, 100};
public static int moneyNum(int money){
//从大到小计算
for(int i = N-1; i >= 0; i--){
int temp =min(money/value[i], count[i]);
money = money -temp*value[i];
num+= temp;
}
if(money > 0)
num = -1;
return num;
}
/**
* @Title: min
* @Description: TODO
* @param i
* @param j
* @return
* @return int
* @throws
*/
private static int min(int i, int j) {
// TODO Auto-generated method stub
if(i <= j)
return i;
else
return j;
}
/**
*@Title: main
*@Description:测试用例
*@param args
*@return void
*@throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int money = 254;
int res = moneyNum(money);
if(res == -1)
System.out.println("No");
else
System.out.println(res);
}
}
用贪心算法的思想,很显然,每一步尽可能用面值大的纸币即可。在日常生活中我们自然而然也是这么做的。
在程序中已经事先将Value按照从小到大的顺序排好。
具体实现代码如下 所示:
/**
*@Title: LightMoney.java
*@Package greedyalgorithm
*@Description: TODO
*@author peidong
*@date 2017-5-16 上午9:13:12
*@version V1.0
*/
packagegreedyalgorithm;
/**
* @ClassName: LightMoney
* @Description: 钱币找零问题
* @date 2017-5-16 上午9:13:12
*
*/
publicclass LightMoney {
public static int N = 7;
public static int num = 0;
public static int[] count = {3, 0, 2, 1,0, 3, 5};
public static int[] value = {1, 2, 5, 10,20, 50, 100};
public static int moneyNum(int money){
//从大到小计算
for(int i = N-1; i >= 0; i--){
int temp =min(money/value[i], count[i]);
money = money -temp*value[i];
num+= temp;
}
if(money > 0)
num = -1;
return num;
}
/**
* @Title: min
* @Description: TODO
* @param i
* @param j
* @return
* @return int
* @throws
*/
private static int min(int i, int j) {
// TODO Auto-generated method stub
if(i <= j)
return i;
else
return j;
}
/**
*@Title: main
*@Description:测试用例
*@param args
*@return void
*@throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int money = 254;
int res = moneyNum(money);
if(res == -1)
System.out.println("No");
else
System.out.println(res);
}
}
相关文章推荐
- 贪心算法 -- 钱币找零问题
- 贪心算法之钱币找零问题
- 算法导论第十六章贪心算法-思考题16-1找零问题
- 贪心之钱币找零问题
- 使用贪心算法实现硬币找零问题
- 贪心算法实例(一):多任务分配问题
- JS使用贪心算法解决找零问题示例
- 从硬币找零问题:看分治/动态规划/贪心算法的区别与联系
- 钱币找零问题
- 贪心算法--钱币找零问题
- 贪心算法实例(四):部分背包问题
- 采用C++实现区间图着色问题(贪心算法)实例详解
- 贪心算法实例(三):多机调度问题
- 一个自动居中的导航条实例与相关问题 DIV+CSS
- 如何解决 JMeter 通过 JDBC 访问 Oracle 和 MySQL 的问题 (留言中有 Test Plan 实例下载)
- 关于实例字段警告问题
- 贪心算法在背包背包问题中应用的探讨
- 处理JSP中文跨页面间传参乱码的问题有两种方法实现方法(每个搞JSP的最常遇到的问题之一) 实例版:)
- 硬币找零问题(动态规划)
- 如何解决启动用户实例的进程时出错的问题(Sql Server Exoress)[转载]