牛客网 华中科技大学 矩阵转置

传送门

题目描述
输入一个N*N的矩阵,将其转置后输出。要求:不得使用任何数组(就地逆置)。

输入描述:
输入的第一行包括一个整数N,(1<=N<=100),代表矩阵的维数。
接下来的N行每行有N个整数,分别代表矩阵的元素。

输出描述:
可能有多组测试数据,对于每组数据,将输入的矩阵转置后输出。

示例1

输入

3
1 2 3
4 5 6
7 8 9

输出

1 4 7
2 5 8
3 6 9

直接上代码

#include<bits/stdc++.h> 

using namespace  std;

int main(){
	int arr[100][100];
	int N,num;
	while (cin>>N)
	{
		for (int i=0;i<N;i++){
			for(int j=0;j<N;j++)
			{
				cin>>num;
				arr[j][i]=num;
			}
		}
			
			for (int oi=0;oi<N;oi++)
			{
				for(int oj=0;oj<N;oj++)
				{
					if (N-1==oj)
						cout<<arr[oi][oj];
					else
						cout<<arr[oi][oj]<<" ";
				}
				cout<<endl;
			}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37774171/article/details/85083955
今日推荐