###### 动态规划之找零钱问题
import java.util.ArrayList;

public class Coin {
/**
* 凑够sum至少需要多少coin
* @param coins coin面值
* @param sum
* @return
*/
public static int[] getLeastNum(int[] coins, int sum) {
int[] d = new int[sum + 1];
d[0] = 0;

for (int i = 1; i <= sum; i++) {
int min = i - coins[0] >= 0 ? d[i - coins[0]] + 1 : 0;
for (int j = 1; j < coins.length; j++) {
if ((i - coins[j]) >= 0 && min > d[i - coins[j]] + 1) {
min = d[i - coins[j]] + 1;
}
}
d[i] = min;
}
return d;
}

public static ArrayList<Integer>[] getLeastNumAndValue(int[] coins, int sum) {
ArrayList[] ret = new ArrayList[sum + 1];

for (int i = 0; i < ret.length; i++) {
ret[i] = new ArrayList<Integer>();
}

int[] d = new int[sum + 1];
d[0] = 0;

for (int i = 1; i <= sum; i++)
{
int min = i - coins[0] >= 0 ? d[i - coins[0]] + 1 : 0;
boolean flag = true;
int j = 1;
int min_ = 1;
for (; j < coins.length; j++)
{
if ((i - coins[j]) >= 0 && min >= d[i - coins[j]] + 1)
{
min = d[i - coins[j]] + 1;
min_ = j;
flag = false;
}
}
d[i] = min;
if (flag) {
if (i - coins[0] > 0)
} else {
if (i - coins[min_] > 0)
}
}
return ret;
}

public static void main(String[] args) {
int coins[] = {1, 3, 5};
int[] ret = getLeastNum(coins, 21);
ArrayList<Integer>[] nums = getLeastNumAndValue(coins, 21);
for (int i = 1; i < ret.length; i++) {
System.out.print("i = " + i + "  value = " + ret[i] + "  nums = ");
ArrayList<Integer> r = nums[i];
for (int j = 0; j < r.size(); j++) {
System.out.print(r.get(j) + " ");
}
System.out.println();
}

}

}


#### 动态规划算法-----找零钱问题（求最优解）

2017-08-03 10:58:26

#### 动态规划——找零钱问题

2009-08-21 23:13:00

#### 找零钱-动态规划

2014-10-28 22:27:15

#### 动态规划--找零钱有多少种方法

2017-09-07 16:27:05

#### 动态规划算法思想解决找零钱问题

2017-10-16 14:20:54

#### 动态规划解决找零钱问题

2015-09-09 19:03:54

#### 【算法】找零钱-动态规划实现过程解析

2017-09-29 12:07:25

#### 找零钱3种方法

2018-02-26 20:49:22

#### 动态规划找零钱问题

2017-02-28 15:25:43

#### 兑换<em>零钱</em>问题的<em>动态规划算法</em>研究

2018年05月07日 00:00