题解 | #矩阵交换#
矩阵交换
http://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817
不进行实际的交换 只记录
#include <iostream> #include <iomanip> using namespace std; int main(){ int i,j,k,n,m,p,q,temp; cin>>n>>m; int a[n+1][m+1],row[n+1],col[m+1]; for(i=1;i<=n;i++) row[i]=i; for(i=1;i<=m;i++) col[i]=i; char t; for(i=1;i<=n;i++){ for(j=1;j<=m;j++) cin>>a[i][j]; } cin>>k; while(k>0){ k--; cin>>t>>p>>q; if(t=='r'){ temp=row[p]; row[p]=row[q]; row[q]=temp; } else if(t=='c'){ temp=col[p]; col[p]=col[q]; col[q]=temp; } } for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ cout<<a[row[i]][col[j]]; if(j<m) cout<<' '; else cout<<endl; } } return 0; }