2 fanxingsikao fanxingsikao 于 2014.12.14 09:51 提问

java用编写一个4*4矩阵程序,并要求行列能够互换

各位大神帮帮忙,请编写一个程序,要求能够对一个4*4的矩阵进行行列互换,非常感谢!

2个回答

lxq_xsyu
lxq_xsyu   Rxr 2014.12.14 11:40
    public static void main(String[] args) {
        int[][] a = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}, 
            {13, 14, 15, 16}
        };
        int[][] b = new int[4][4];

        int i, j;
        System.out.println("源数组:");
        for(i=0; i<4; i++){
            for(j=0;j<4;j++){
                System.out.print(a[i][j] + "\t");
                b[j][i]=a[i][j];
            }
            System.out.println();
        }

        System.out.println("转换后数组:");
        for(i=0;i<4;i++) {
            for(j=0;j<4;j++){
                System.out.print(b[i][j] + "\t");
            }
            System.out.println();
        }

    }
javanet2
javanet2   2014.12.15 17:13

public static void main(String[] args) {
List list=new ArrayList();

     for (int i = 1; i <=22; i++) {
        list.add(i);
    }

     for (int i = 0; i < list.size(); i++) {
         int val=list.get(i);
        System.out.print(val);
        if(i%4!=3){
            System.out.print("\t");
        }else{
            System.out.println();
        }
    }

     System.out.println();
     System.out.println();
     int n=list.size()/4+(list.size()%4==0?0:1);
     System.out.println(n);
     System.out.println();
     System.out.println();

     for (int i = 0; i <n; i++) {
        for (int j = i; j < list.size(); j=j+n) {
            System.out.print(list.get(j)+"\t");
        }
        System.out.println();

    }

}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!