题面

计算矩阵A的转置矩阵。

输入

首先输入一个正整数t(1<= t <= 10010)
接下来t组测试数据,每组测试数据首先给2个整数n和m,表示矩阵A有n行m列。(2<= n,m<= 22)
然后有n*m个整数,表示矩阵A的元素。(0<= a[i][j] <= 73)

输出

对于每个测试数据,输出相应的转置矩阵。注意输出格式:每行末尾不能有多余的空格,每个数以%3d的格式输出。

样例输入

1
4 3
1 9 8
5 3 2
8 4 3
2 6 0

样例输出

  1   5   8   2
  9   3   4   6
  8   2   3   0

代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int t;
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n, m;
            sc.nextLine();
            n = sc.nextInt();
            m = sc.nextInt();

            int [][] arr = new int[10][30];

            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < m; ++j) {
                    arr[i][j] = sc.nextInt();
                }
            }

            for(int i = 0; i < m; ++i) {
                for(int j = 0; j < n; ++j) {
                    System.out.printf("%3d", arr[j][i]);
                }
                System.out.println();
            }
        }
    }
}

立志成为一名攻城狮