close
Content
已知一(m x n)矩陣A,我們常常需要用到另一個將A中之行與列調換的矩陣。這個動作叫做矩陣的翻轉。舉例來說,若
A = [ 3 1 2 ] 8 5 4
則
AT = [ 3 8 ] 1 5 2 4
現在 請您針對所讀取到的矩陣進行翻轉。
Input
第一行會有兩個數字,分別為 列(row)<100 和 行(column)<100,緊接著就是這個矩陣的內容
Output
直接輸出翻轉後的矩陣
Sample Input #1
2 3 3 1 2 8 5 4
Sample Output #1
3 8 1 5 2 4
C
#include <stdio.h> int main() { int i,j,a,b; int m[100][100]={0}; while(scanf("%d%d",&a,&b)!=-1) { for(i=0;i<a;i++) { for(j=0;j<b;j++) { scanf("%d ",&m[i][j]); } } for(i=0;i<b;i++) { for(j=0;j<a;j++) { printf("%d ",m[j][i]); } printf("\n"); } } return 0; }
Python
from sys import stdin for s in stdin: r,c=map(int,s.split()) arr=[] for _ in range(r): arr.append(stdin.readline().strip().split()) for i in range(c): for j in range(r): print(arr[j][i],end=" ") print(f" ")
文章標籤
全站熱搜