1 问题描述
现需找零金额为n,则最少需要用多少面值为d1 < d2 < d3 < … < dm的硬币?(PS:假设这m种面值d1 < d2 < d3 < … < dm的硬币,其中d1 = 1,且每种硬币数量无限可得)
2 解决方案
2.1 动态规划法
本文编码思想参考自《算法设计与分析基础》第三版,具体讲解如下:
package com.liuzhen.chapter8;
public class ChangeMaking {
public void getChangeMakingN(int[] coinType,int n){
int[] minNumber = new int[n+1]; //初始化后,所有元素均为0,其中minNumber[0] = 0,表示无须找零
int[] tempMinJ = new int[n+1]; //tempMinJ[0]在此处无含义
for(int i = 1;i <= n;i++){
int j = 0;
int tempJ = -1; //用于h获取minNumber[i]最小值中当前新使用的硬币面值数组下标
int temp = Integer.MAX_VALUE; //计算当前minNumber[i]最小值,初始化int类型最大值
while(j < coinType.length && i >= coinType[j]){
if(minNumber[i-coinType[j]] + 1 < temp){
temp = minNumber[i-coinType[j]] + 1;
tempJ = j;
}
j++;
}
minNumber[i] = temp;
tempMinJ[i] = tempJ;
}
System.out.println("给定硬币面值种类依次为:");
for(int i = 0;i < coinType.length;i++)
System.out.print(coinType[i]+" ");
System.out.println("
找零大小从1到"+n+"的最少硬币组合数目为:");
for(int i = 1;i < minNumber.length;i++)
System.out.print(minNumber[i]+" ");
System.out.println("
对应找零大小从1到"+n+"新增的硬币数组下标为:");
for(int i = 1;i < tempMinJ.length;i++)
System.out.print(tempMinJ[i]+" ");
System.out.println("
对应找零大小从1到"+n+"新增的硬币数组下标对应的硬币面值为:");
for(int i = 1;i < tempMinJ.length;i++)
System.out.print(coinType[tempMinJ[i]]+" ");
System.out.println("
找零大小为"+n+"的硬币组合最少数目为:"+minNumber[minNumber.length-1]);
System.out.print("找零大小为"+n+"的硬币组合最少数目对应的硬币面值依次为:");
int needN = n;
int minJ = tempMinJ.length-1;
while(needN > 0){
System.out.print(coinType[tempMinJ[minJ]]+" ");
needN = needN - coinType[tempMinJ[minJ]];
minJ = needN;
}
}
public static void main(String[] args){
ChangeMaking test = new ChangeMaking();
int[] coinType = {1,3,4};
test.getChangeMakingN(coinType, 6);
}
}
运行结果:
给定硬币面值种类依次为:
1 3 4
找零大小从1到6的最少硬币组合数目为:
1 2 1 1 2 2
对应找零大小从1到6新增的硬币数组下标为:
0 0 1 2 0 1
对应找零大小从1到6新增的硬币数组下标对应的硬币面值为:
1 1 3 4 1 3
找零大小为6的硬币组合最少数目为:2
找零大小为6的硬币组合最少数目对应的硬币面值依次为:3 3