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:
- import java.util.Scanner;
- public class Main{
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- while(sc.hasNext()){
- int n = sc.nextInt();
- int m = sc.nextInt();
- int arr[][] = new int[n][m];
- for(int a=0;a<n;a++)
- for(int b=0;b<m;b++){
- arr[a][b] = sc.nextInt();
- }
- for(int a=0;a<m;a++){
- for(int b=0;b<n;b++){
- if(b==0)
- System.out.print(arr[b][a]);
- else
- System.out.print(" "+arr[b][a]);
- }
- System.out.println();
- }
- }
- }
- }
文章標籤
全站熱搜