硬币找零算法
来源:互联网 发布:通达信炒股软件 编辑:程序博客网 时间:2024/06/01 07:59
public final class MakeChange
{
// Dynamic programming algorithm to solve change making problem.
// As a result, the coinsUsed array is filled with the
// minimum number of coins needed for change from 0 -> maxChange
// and lastCoin contains one of the coins needed to make the change.
public static void makeChange( int [ ] coins, int differentCoins,
int maxChange, int [ ] coinsUsed, int [ ] lastCoin )
{
coinsUsed[ 0 ] = 0; lastCoin[ 0 ] = 1;
for( int cents = 1; cents <= maxChange; cents++ )
{
int minCoins = cents;
int newCoin = 1;
for( int j = 0; j < differentCoins; j++ )
{
if( coins[ j ] > cents ) // Cannot use coin j
continue;
if( coinsUsed[ cents - coins[ j ] ] + 1 < minCoins )
{
minCoins = coinsUsed[ cents - coins[ j ] ] + 1;
newCoin = coins[ j ];
}
}
coinsUsed[ cents ] = minCoins;
lastCoin[ cents ] = newCoin;
}
}
{
// Dynamic programming algorithm to solve change making problem.
// As a result, the coinsUsed array is filled with the
// minimum number of coins needed for change from 0 -> maxChange
// and lastCoin contains one of the coins needed to make the change.
public static void makeChange( int [ ] coins, int differentCoins,
int maxChange, int [ ] coinsUsed, int [ ] lastCoin )
{
coinsUsed[ 0 ] = 0; lastCoin[ 0 ] = 1;
for( int cents = 1; cents <= maxChange; cents++ )
{
int minCoins = cents;
int newCoin = 1;
for( int j = 0; j < differentCoins; j++ )
{
if( coins[ j ] > cents ) // Cannot use coin j
continue;
if( coinsUsed[ cents - coins[ j ] ] + 1 < minCoins )
{
minCoins = coinsUsed[ cents - coins[ j ] ] + 1;
newCoin = coins[ j ];
}
}
coinsUsed[ cents ] = minCoins;
lastCoin[ cents ] = newCoin;
}
}
- 硬币找零算法
- 算法-硬币找零
- 硬币找零(贪心算法)
- 使用贪心算法实现硬币找零问题
- 贪心算法解硬币找零问题
- 算法练习 动态规划 硬币找零
- 动态规划算法求解硬币找零问题
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- 硬币找零
- WebService大讲堂之Axis2(6):跨服务会话(Session)管理
- 创建新的文章
- 第一篇
- WM:第一个今日项目
- 神侃继续中
- 硬币找零算法
- 很菜的鸟,很菜的鸟
- java多线程(转)
- 5月11号 周一 面向对象高级(下)
- 100句记完700个单词
- 喜迎3G发牌,乐成数字通信学院隆重推出2009新版课程
- 可以拖放的菜单栏
- 09.05.11
- BTV5《首都经济报道》——深度采访乐成数字通信学院