题目描述
输入一个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<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
const int N = 100;
int main()
{
int matrix[N][N];
int n;
while(cin >> n){
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < n ; j++){
cin >> matrix[i][j];
}
}
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < n ; j++){
cout << matrix[j][i] << " ";
}
cout << endl;
}
}
return 0;
}
1296

被折叠的 条评论
为什么被折叠?



