10: Matrix Transpose

Total time limit: 1000ms memory limit: 65536kB
Description
Enter a matrix of n rows and columns of A m output its transpose AT.

Input
The first line contains two integers n and m, the number of rows and columns of the matrix A. 1 <= n <= 100,1 < = m <= 100.
Next n lines of m integers, A represents an element of the matrix. Separated by a single space between two adjacent integers, each element is between 1 and 1000.
Output
m lines of n integers, the A matrix transpose. Separated by a single space between two adjacent integers.
Sample input
. 3. 3
. 1 2. 3
. 4. 5. 6
. 7. 8. 9
sample output
. 1. 4. 7
2. 5. 8
. 3. 6. 9

#include<iostream>
using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	
	int a[n+5][m+5];
	//输入矩阵 
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	cin>>a[i][j];
	
	//按列输出,以前是按行输入,按行输出,
	//转置过后是按行输入,按列输出 
	//此时即为按列输出 
	for(int j=1;j<=m;j++){
		for(int i=1;i<=n;i++)
		cout<<a[i][j]<<' '; 
		
		cout<<endl;
	}
	
	return 0;
}
Published 36 original articles · won praise 0 · Views 350

Guess you like

Origin blog.csdn.net/weixin_44437496/article/details/104008090