Die Aufgabe lautet aus einem int array rekursiv alle möglichen kombinationen herauszufinden.
Zum Beispiel: [1,2,3,4]
11 , 12 , 13 , 14 , 22 , 23 , 24 , 33 , 34 , 44 (mit Wiederholungen) oder
12 , 13 , 14 , 23 , 24 , 34 (ohne Wiederholungen)
es darf zusätzlich maximal eine Schleife zusätzlich benutzt werden...
Ich hänge jedoch komplett auf dem schlauch, wie ich das lösen soll.
Ich hoffe es kann mir jemand einen Denkanstoß geben, wie ich an diese Aufgabe angehen soll.
Vielen Dank schonmal im Vorraus
Das sind meine Gedanken, jedoch funktioniert keine davon^^:
(n beschreibt die anzahl der elemente der kombinationen)
public class AusgabeVonKombinationen {
public static void printCombinations(int[] array, int n) {
int[] tempArray = new int[array.length-1];
for (int i = 0; i < array.length; i++) {
System.out.print(array + " " + array);
// if(i > 0) {
// tempArray[i-1] = array;
// }
//
// }
// if(n>1) {
// printCombinations(tempArray, n-1);
}
// int temp = array[array.length];
// for (int i = 0; i <= array.length-1; i++) {
// int combi = array;
// System.out.println(temp + " " + combi);
// printCombinations(array.length-1, n);
// }
}
public static void main(String[] args) {
int[] a = new int[] { 1, 2, 3, 4 };
int n = 2;
// int n = new Random().nextInt(a.length - 1 + 1) + 1;
printCombinations(a, n);
}
}
Zum Beispiel: [1,2,3,4]
11 , 12 , 13 , 14 , 22 , 23 , 24 , 33 , 34 , 44 (mit Wiederholungen) oder
12 , 13 , 14 , 23 , 24 , 34 (ohne Wiederholungen)
es darf zusätzlich maximal eine Schleife zusätzlich benutzt werden...
Ich hänge jedoch komplett auf dem schlauch, wie ich das lösen soll.
Ich hoffe es kann mir jemand einen Denkanstoß geben, wie ich an diese Aufgabe angehen soll.
Vielen Dank schonmal im Vorraus
Das sind meine Gedanken, jedoch funktioniert keine davon^^:
(n beschreibt die anzahl der elemente der kombinationen)
public class AusgabeVonKombinationen {
public static void printCombinations(int[] array, int n) {
int[] tempArray = new int[array.length-1];
for (int i = 0; i < array.length; i++) {
System.out.print(array + " " + array);
// if(i > 0) {
// tempArray[i-1] = array;
// }
//
// }
// if(n>1) {
// printCombinations(tempArray, n-1);
}
// int temp = array[array.length];
// for (int i = 0; i <= array.length-1; i++) {
// int combi = array;
// System.out.println(temp + " " + combi);
// printCombinations(array.length-1, n);
// }
}
public static void main(String[] args) {
int[] a = new int[] { 1, 2, 3, 4 };
int n = 2;
// int n = new Random().nextInt(a.length - 1 + 1) + 1;
printCombinations(a, n);
}
}