打印大小为m * n的矩阵中的所有元素组合

时间:2015-03-22 04:21:10

标签: java matrix

以m * n

的矩阵打印所有元素组合

示例示例:

1  3  5  
2  6  7

预期产出:

1 , 3 , 5 
1 , 3 , 7 
1 , 6 , 5
1 , 6 , 7 
2 , 3 , 5
2 , 3 , 7
2 , 6 , 5
2 , 6 , 7

规则:

  • 每个组合从矩阵的左边开始,向右前进。它可能会切换行。
  • 每个组合的元素数应等于列数。
  • 组合不能使同一列中的元素出现两次。
  • 列数和行数可能会有所不同。所以解决方案必须是通用的。

    import java.util.Scanner;
    
    class Combination {
    public static void main(String args[]) {
    int row, col, i, j;
    
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the number of rows and columns of matrix:\n");
    row = in.nextInt();
    col = in.nextInt();
    
    int first[][] = new int[row][col];
    System.out.println("Enter the elements if matric m*n:\n");
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            first[i][j] = in.nextInt();
        }
    }
    System.out.println("Matrix:\n");
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            System.out.print(first[i][j] + "\t");
        }
        System.out.println();
    }
    // Final Logic from here...
    System.out.println("\nOut Matrix:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                System.out.println(first[i][0] + "," + first[j][1] + ","
                        + first[k][2]+"\n");
            }
        }
    }
     /* while (i < 2) {
        j = 0;
        while (j < 2) {
            k = 0;
            while (k < 2) {
                System.out.println(first[i][0] + "," + first[j][1] + ","
                        + first[k][2]);
                k++;
            }
            j++;
        }
        i++;
    }*/
    in.close();
    }
    }
    
适用于特定输入,但不能动态....需要帮助..

先谢谢......

4 个答案:

答案 0 :(得分:2)

您可以按如下方式使用递归:

...
    // Final Logic from here...
    System.out.println("\nOut Matrix:\n");
    int[] outputRow = new int[col];
    print(0, row, col, first, outputRow);

}

private static void print(int j, int row, int col, int[][] first, int[] outputRow) {
    for (int i = 0; i < row; i++) {
        outputRow[j] = first[i][j];
        // recursively continue to populate outputRow until we reach the last column (j == col -1)
        if (j < col - 1) {
            print(j + 1, row, col, first, outputRow);               
        }
        // we have reached the last column (j == col -1) so now we could print current permutation
        if (j == col - 1) {
            for (int k = 0; k < col; k++) {
                System.out.print(" " + outputRow[k]);                   
            }
            System.out.println();                   
        }
    }
}

这里我们处理每个以j==0开头的递归调用的一列。

outputRow存储当前的排列并以递归方式更新。

当我们递归到达最后一列时,是时候打印当前的排列了。

答案 1 :(得分:2)

这是一种可行的方法

void printCombos(){
    visit(0,-1,"");
}

void visit(int r,int c,String s){
    if(c!=a[0].length-1)
        for(int i=0;i<a.length;i++)
            visit(i,c+1,s+" - "+a[i][c+1]);
    else
        System.out.println(s);
}

将矩阵视为深入访问的树。给定一个假想的根*这些是边(*,1) - (*,2) - (1,3) - (1,6) - (2,3) - (2,6)等等

* --- 1 -- 3 -- 5
  \     \/   \/
   \    /\   /\
    \ 2 -- 6 -- 7

以5和7为叶子。

答案 2 :(得分:0)

首先再创建一个方法:

private static void increasePointerArray(int[] poinerArray, int row) 
{
    for (int i = poinerArray.length-1; i >= 0; i--) {
        if(poinerArray[i] == row-1) {
            continue;
        }
        else {
            poinerArray[i] = poinerArray[i] +1;
            for (int j = i+1; j < poinerArray.length; j++) {
                poinerArray[j] = 0;
            }
            break;
        }
    }
}

现在在最后的逻辑部分放置代码:

int[] poinerArray = new int[col];
int[] MaxArray = new int[col];
List<int[]> resultList = new ArrayList<int[]>();

Arrays.fill(poinerArray, 0);
Arrays.fill(MaxArray, row-1);

while(!Arrays.equals(poinerArray, MaxArray)) {
    resultList.add(poinerArray.clone());
    increasePointerArray(poinerArray, row);
}
resultList.add(poinerArray.clone());

System.out.println("Printing desired result : ");

for (int[] ks : resultList) {
    StringBuffer sb = new StringBuffer();
    for (j = 0; j < col; j++) {
        sb.append(first[ks[j]][j]+"\t");
    }
    System.out.println(sb.toString());
    sb = null;
}

答案 3 :(得分:0)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Permutations{
    public static String strings="";
    public static ArrayList<String> out=new ArrayList<String>();
    public static void gen(ArrayList<ArrayList<String>> x,int index){
        for(int i=0;i<x.size();i++){
            if(i>0){
                String[] parts=strings.split(",");
                strings="";
                for(int k=0;k<parts.length;k++){
                    if(k==index)
                        break;
                    strings=strings+parts[k]+",";
                }
            }
            if(index==x.get(0).size()-1){
                strings=strings+(x.get(i).get(index));
                out.add(strings);
            }
            else
                strings=strings+(x.get(i).get(index))+",";
            if(index+1<=x.get(0).size()-1)
                gen(x,index+1);
        }
    }
    public static void main(String[] args) throws IOException{
        ArrayList<ArrayList<String>> x=new ArrayList<ArrayList<String>>();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String line;
        while(true){
            line=br.readLine();
            if(line.contentEquals("")) break;
            String[] parts=line.split(" ");
            x.add(new ArrayList<String>());
            for(int i=0;i<parts.length;i++){
                x.get(x.size()-1).add(parts[i]);
            }
        }
        gen(x,0);
        for(int i=0;i<out.size();i++){
            System.out.println(out.get(i));
        }
    }
}

此代码有效。它非常通用,很容易理解。我在2D数组上进行了列式排列。

相关问题