Problem C: 矩阵转置

Description

输入N*N的矩阵,输出它的转置矩阵。

Input

第一行为整数N。 
接着是一个N*N的矩阵。

Output

转置矩阵,注意,每两个数之间用一个空格隔开,行末没有空格

Sample Input

21 21 2

Sample Output

1 12 2

import java.util.Scanner;
public class Main {
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         int N = sc.nextInt();
         int[][] a = new int[N][N];
         for (int i = 0; i < N; i++) {
             for (int j = 0; j < N; j++) {
                 a[i][j] = sc.nextInt();
             }
         }
         int[][] a2 = new int[N][N];
         int[] a3 = new int[N];
         for (int i = 0; i < a.length; i++) {
             for (int j = 0; j < a[i].length; j++) {
                 a2[i][j] = a[j][i];
             }
         }
         for (int i = 0; i < a2.length; i++) {
             for (int j = 0; j < a2[i].length - 1; j++) {
                 System.out.print(a2[i][j] + " ");
             }
 
             for (int p = 0; p < N; p++) {
                 a3[p] = a2[i][p];
             }
 
             System.out.print(a3[N - 1]);
             System.out.println();
 
         }
 
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值