题解 | #矩阵交换#
矩阵交换
https://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817
#include <stdio.h> int main() { int NowArr[10][10]; int n,m = 0; scanf("%d%d",&n,&m); int i,j; for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&NowArr[i][j]); } } int k,f,w= 0; scanf("%d",&k); for(i=0;i<k;i++) { int a,b = 0; char p; scanf(" %c %d %d",&p,&a,&b); if(p=='r') { for(f=0;f<m;f++) { int mid = 0; mid = NowArr[b-1][f]; NowArr[b-1][f] = NowArr[a-1][f]; NowArr[a-1][f] = mid; } } else if(p=='c') { for(w=0;w<n;w++) { int mid = 0; mid = NowArr[w][b-1]; NowArr[w][b-1] = NowArr[w][a-1]; NowArr[w][a-1] = mid; } } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { printf("%d ",NowArr[i][j]); if(j==m-1) printf("\n"); } } return 0; }