-
arr货币数组,其中值都是整数。再给定一个整数aim,每个值都被认为是一张货币,即便值相同的货币也认为每一张都是不同的,返回amin的方法数
-
arr货币数组,其中值都是不重复整数。再给定一个整数aim,每个货币的张数是无限的,返回amin的方法数
-
arr货币数组,其中值可以是是重复整数。再给定一个整数aim,每个相同值都被认为是同一种货币,返回amin的方法数
-
arr货币数组,其中值都是不重复整数。再给定一个整数aim,每个货币的张数是无限的,返回构成aim的最少张数
从尝试模型来看
注:相同面值不同货币,相同面值相同货币,如果面值为2的货币有4张,相同面值相同货币,那么每个货币都有选和不选,面临 4 *2 =8个递归,而相同面值相同货币,每张性质一样,所以只能有4个递归
从尝试代码来看(尝试模型导致)
public static int process(int[] arr, int index, int rest) {
if (rest < 0) {
return 0;
}
if (index == arr.length) { // 没钱了!
return rest == 0 ? 1 : 0;
}
return process(arr,index+1,rest)+process(arr,index+1,rest-arr[index]);
}
public static int process(int[] arr, int index, int rest) {
if (index == arr.length) {
return rest == 0 ? 1 : 0;
}
int ways = 0;
for (int zhang = 0; zhang * arr[index] <= rest; zhang++) {
ways += process(arr, index + 1, rest - (zhang * arr[index]));
}
return ways;
}
public static int process(int[] coins, int[] num, int index, int rest) {
if (index == coins.length) {
return rest == 0 ? 1 : 0;
}
int ways = 0;
for (int zhang = 0; zhang * coins[index] <= rest && zhang <= num[index]; zhang++) {
ways += process(coins, num, index + 1, rest - (zhang * coins[index]));
}
return ways;
}
public static int process(int[] arr, int index, int rest) {
if (index == arr.length) {
return rest == 0 ? 0 : Integer.MAX_VALUE;
} else {
int ans = Integer.MAX_VALUE;
for (int zhang = 0; zhang * arr[index] <= rest; zhang++) {
int next = process(arr, index + 1, rest - zhang * arr[index]);
if (next != Integer.MAX_VALUE) {
ans = Math.min(ans, zhang + next);
}
}
return ans;
}
}