-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackTrackCoins.java
More file actions
45 lines (42 loc) · 1.46 KB
/
BackTrackCoins.java
File metadata and controls
45 lines (42 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
import java.util.ArrayList;
public class BackTrackCoins {
public static int[] coins = {1, 5, 10, 20, 50, 100};
public static int ans = 0;
public static void back(int remain, int depth, int[] coinNum, int preCoin, ArrayList<Integer> path)
{
if(remain == 0){
ans += depth;
System.out.println(path);
System.out.println("depth="+depth);
}else{
for(int i = 0; i < 6; i++){
if(coinNum[i] > 0 && coins[i] >= preCoin && remain-coins[i] >= 0){
coinNum[i]--;
path.add(coins[i]);
back(remain-coins[i], depth+1, coinNum, coins[i], path);
path.remove(path.lastIndexOf(coins[i]));
coinNum[i]++;
}
}
}
}
public static String process(String num1, String num2)
{
String nums[] = num1.split(" ");
int[] coinNum = new int[6];
for(int i = 0; i < 6; i++){
coinNum[i] = Integer.parseInt(nums[i]);
}
int total = Integer.parseInt(num2);
back(total, 0, coinNum, 0, new ArrayList<Integer>());
return ans+"";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String strValueSequences = sc.nextLine();
String strChargeNum = sc.nextLine();
String sum = process(strValueSequences, strChargeNum);
System.out.println(sum);
}
}