题意:给定一个n×m矩阵相乘,求它的转置。
思路:
n=2, m=4
(1,1) (1,2) (1,3) (1,4)
(2,1) (2,2) (2,3) (2,4)
转置后:
(1,1) (2,1)
(1,2) (2,2)
(1,3) (2,3)
(1,4) (2,4)
就把遍历行和列的循环交换一下就可以了
代码:
#include<bits/stdc++.h>
using namespace std;
int a[25][25];
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;++i) for(int j=0;j<m;++j) cin>>a[i][j];
for(int j=0;j<m;++j){
bool first=1;
for(int i=0;i<n;++i){
if(first) first=0;
else cout<<" ";
cout<<a[i][j];
}
puts("");
}
return 0;
}