close

題目取自於ITSA協作平台
題目:轉置矩陣

 

問題描述:

有一矩陣 N,M ,將其利用轉置方法改變矩陣。

輸入說明

輸入資料第一列為矩陣大小, N 、 M ,然後輸入矩陣數值。若 N 、 M 輸入 0 則結束程式。

輸出說明

得到轉置矩陣。

Sample Input

Sample Output

2 3

2 1 3

8 7 9

2 8

1 7

3 9

3 3

1 2 3

8 4 9

1 0 3

1 8 1

2 4 0

3 9 3

5 5

1 1 0 0 0

0 0 0 1 1

1 0 1 0 1

0 1 0 1 0

1 0 1 0 0

1 0 1 0 1

1 0 0 1 0

0 0 1 0 1

0 1 0 1 0

0 1 1 0 0

 

 

Code:

  1. import java.util.Scanner;  
  2. public class Main{  
  3.     public static void main(String args[]){  
  4.         Scanner sc = new Scanner(System.in);      
  5.         while(sc.hasNext()){  
  6.             int n = sc.nextInt();  
  7.             int m = sc.nextInt();  
  8.             int arr[][] = new int[n][m];  
  9.             for(int a=0;a<n;a++)  
  10.                 for(int b=0;b<m;b++){  
  11.                     arr[a][b] = sc.nextInt();  
  12.                 }                     
  13.             for(int a=0;a<m;a++){  
  14.                 for(int b=0;b<n;b++){  
  15.                     if(b==0)  
  16.                         System.out.print(arr[b][a]);  
  17.                     else  
  18.                         System.out.print(" "+arr[b][a]);  
  19.                 }  
  20.                 System.out.println();  
  21.             }  
  22.         }  
  23.     }  
  24. }  
arrow
arrow
    文章標籤
    JAVA ITSA 轉置矩陣
    全站熱搜

    ニャー提督 發表在 痞客邦 留言(0) 人氣()