私はこの質問のさまざまなバージョンを見てきましたが、決して良い答えはありません。私はMXN配列を持ち、Mサイズのすべての可能な組み合わせを返したいと思います。例をあげましょう.3X3の配列があります。結果は27通りあります。私はここで再帰的な方法を試みていますが、今まで運がありませんでした。Javaの行列からすべての組み合わせを返します
-1
A
答えて
1
このプログラムでお手伝いしてください。私はこの行列を使用しました
int[][] matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
出力は以下のように27通りあります。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
private static List<int[]> combine(int[][] matrix) {
int sizeArray[] = new int[matrix.length];
int counterArray[] = new int[matrix.length];
int total = 1;
for (int i = 0; i < matrix.length; ++i) {
sizeArray[i] = matrix[i].length;
total *= matrix[i].length;
}
List<int[]> list = new ArrayList<>(total);
StringBuilder sb;
for (int count = total; count > 0; --count) {
sb = new StringBuilder();
for (int i = 0; i < matrix.length; ++i) {
sb.append(matrix[i][counterArray[i]]);
}
int tmpi[] = new int[sb.toString().length()];
for (int tmp = 0; tmp < sb.toString().length(); tmp++) {
tmpi[tmp] = Integer.parseInt("" + sb.toString().toCharArray()[tmp]);
}
list.add(tmpi);
for (int incIndex = matrix.length - 1; incIndex >= 0; --incIndex) {
if (counterArray[incIndex] + 1 < sizeArray[incIndex]) {
++counterArray[incIndex];
break;
}
counterArray[incIndex] = 0;
}
}
return list;
}
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int i = 0;
for (int[] c : (combine(matrix))) {
System.out.println(Arrays.toString(c));
i++;
}
System.out.println(i);
}
}
テスト
[1, 4, 7]
[1, 4, 8]
[1, 4, 9]
[1, 5, 7]
[1, 5, 8]
[1, 5, 9]
[1, 6, 7]
[1, 6, 8]
[1, 6, 9]
[2, 4, 7]
[2, 4, 8]
[2, 4, 9]
[2, 5, 7]
[2, 5, 8]
[2, 5, 9]
[2, 6, 7]
[2, 6, 8]
[2, 6, 9]
[3, 4, 7]
[3, 4, 8]
[3, 4, 9]
[3, 5, 7]
[3, 5, 8]
[3, 5, 9]
[3, 6, 7]
[3, 6, 8]
[3, 6, 9]
27
0
再帰バージョン:
import java.util.Arrays;
public class Matrix {
private static int counter = 0;
private static void combin2(int depth, int[][]matrix, int[] output)
{
int[] row = matrix[depth];
if(depth == 0) {
counter = 0;
output = new int[matrix.length];
System.out.println("matrix length: " + matrix.length);
}
for(int i=0; i<row.length; i++) {
output[depth] = row[i];
if(depth == (matrix.length-1)) {
//print the combination
System.out.println(Arrays.toString(output));
counter++;
} else {
//recursively generate the combination
combin2(depth+1, matrix, output);
}
}
}
public static void main(String[] args) {
int[][] matrix = new int[][] {{1, 2, 3, 4}, {5, 6, 7, 8}};
combin2(0, matrix, null);
System.out.println("counter = " + counter);
System.out.println("");
matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
combin2(0, matrix, null);
System.out.println("counter = " + counter);
System.out.println("");
matrix = new int[][]{{1, 2, 3}, {4,5,6}, {7,8,9}, {10,11,12}};
combin2(0, matrix, null);
System.out.println("counter = " + counter);
System.out.println("");
}
}
出力:
matrix length: 2
[1, 5]
[1, 6]
[1, 7]
[1, 8]
[2, 5]
[2, 6]
[2, 7]
[2, 8]
[3, 5]
[3, 6]
[3, 7]
[3, 8]
[4, 5]
[4, 6]
[4, 7]
[4, 8]
counter = 16
matrix length: 3
[1, 4, 7]
[1, 4, 8]
[1, 4, 9]
[1, 5, 7]
[1, 5, 8]
[1, 5, 9]
[1, 6, 7]
[1, 6, 8]
[1, 6, 9]
[2, 4, 7]
[2, 4, 8]
[2, 4, 9]
[2, 5, 7]
[2, 5, 8]
[2, 5, 9]
[2, 6, 7]
[2, 6, 8]
[2, 6, 9]
[3, 4, 7]
[3, 4, 8]
[3, 4, 9]
[3, 5, 7]
[3, 5, 8]
[3, 5, 9]
[3, 6, 7]
[3, 6, 8]
[3, 6, 9]
counter = 27
matrix length: 4
[1, 4, 7, 10]
[1, 4, 7, 11]
[1, 4, 7, 12]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問題
- 1. Python - ファイルからの行 - すべての組み合わせ
- 2. Javaすべての可能な組み合わせを列から検索
- 3. Matlab:バイナリ行列のすべての組み合わせ
- 4. aaaから~~~までのすべての組み合わせ
- 5. すべての組み合わせを作成するJava
- 6. すべての組み合わせツリーアルゴリズム
- 7. データフレームの列と行のすべての可能な組み合わせを繰り返します。
- 8. すべての組み合わせを検索6 number JAVA
- 9. 列の組み合わせの組み合わせデータフレームの行ではない
- 10. すべての可能な組み合わせのjavaリストジェネレータ
- 11. すべての可能な文字列の組み合わせ
- 12. 可能なすべての組み合わせの組み合わせで複数の列を組み合わせる
- 13. JavaScriptはすべて組み合わせを生成します
- 14. serviceAutoStartProvidersをApp_Offline.htmと組み合わせて実行しますか?
- 15. すべての可能な組み合わせを組み合わせて単語を追加する
- 16. 組み合わせ - 配列からの人をペアにする
- 17. Javaですべての文字/単語の組み合わせを試す
- 18. Smalltalkのコレクションからすべての組み合わせを生成する
- 19. Javaの組み合わせの生成
- 20. perlの変数と文字列の組み合わせを返します
- 21. 配列と組み合わせパターンの組み合わせを見つける
- 22. MySQLの3つのカテゴリからすべての組み合わせを取得
- 23. itertools.combinations()はすべての組み合わせを表示していません
- 24. I、列によって、これらの3つのデータ・フレーム(すなわち、9つの組み合わせ)のすべての可能なペアの組合せをマージしたい複数のデータフレームのすべての可能な組み合わせを
- 25. ビットからすべての組み合わせを取得するには?
- 26. UMLステートダイアグラムからすべての組み合わせを取得する方法
- 27. Java - リストの組み合わせ
- 28. すべての組み合わせを印刷するには?
- 29. すべての組み合わせを生成するバックトラッキングアルゴリズム
- 30. すべての文字の組み合わせを生成
あなたのコードを投稿し、あなたが得たものを説明し、なぜOKではありませんしてください。 –
あなたはそれを試してみましたか? –
なぜ3x3アレイの27の組み合わせがありますか?例えば。各配列要素が2つの値を持つことができれば、2^9 = 512個の置換があります。各配列要素が10個の値を持つことができる場合、10^9 = 1,000,000,000個の置換があります。どのように27の組み合わせしか得られないのですか? – Andreas