public static int wechselgeld(int betrag, int threshold, boolean print) {
if (print) {
System.out.println(betrag);
}
List<Integer> t1 = new LinkedList<>();
List<Integer> t2 = new LinkedList<>();
while (betrag >= threshold) {
betrag -= 8;
t1.add(8);
t2.add(8);
}
for (int i = betrag; i >= 0; i--) {
t2.add(1);
}
int i = wechselgeld(betrag, t1, t2);
if (print) {
System.out.println(i);
System.out.println(t2);
}
return i;
}
public static int wechselgeld(int betrag, List<Integer> temp, List<Integer> best) {
int[] m = { 8, 6, 3, 1 };
for (int i = 0; i < m.length; i++) {
if (betrag - m[i] >= 0) {
temp.add(m[i]);
wechselgeld(betrag - m[i], temp, best);
if (betrag - m[i] == 0 && temp.size() < best.size()) {
best.clear();
best.addAll(temp);
}
temp.remove(temp.size() - 1);
}
}
return best.size();
}
public static void main(String[] args) {
System.out.println(IntStream.rangeClosed(0, 100).map(i -> wechselgeld(i, 12, false)).sum());
System.out.println(IntStream.rangeClosed(0, 100).map(i -> wechselgeld(i, 13, false)).sum());
System.out.println(IntStream.rangeClosed(0, 100).map(i -> wechselgeld(i, 14, false)).sum()); // non-optimal threshold
System.out.println(IntStream.rangeClosed(0, 100).map(i -> wechselgeld(i, 15, false)).sum());
System.out.println(IntStream.rangeClosed(0, 100).map(i -> wechselgeld(i, 18, false)).sum());
System.out.println(IntStream.rangeClosed(0, 100).map(i -> wechselgeld(i, 19, false)).sum()); // optimal threshold
System.out.println(IntStream.rangeClosed(0, 100).map(i -> wechselgeld(i, 20, false)).sum());
wechselgeld(18, 14, true);
wechselgeld(18, 19, true);
}