Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Coins {
- public static int[] V = new int[] {1, 3, 5};
- public static int S = 11;
- public static void main(String[] args) {
- System.out.println(recurse(2, 0));
- }
- public static int recurse(int i, int total) {
- int min = -1;
- int num = 0;
- if (i < 0) return (S - total == 0) ? 0 : -1;
- for (int j = (S - total) / V[i]; j >= 0; j--) {
- int tmp = recurse(i - 1, total + V[i] * j);
- if (tmp >= 0 && (min == -1 || tmp < min)) {
- min = tmp;
- num = j;
- }
- }
- return min + num;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment