#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
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<<’\n’;
}
return 0;
}
2 个赞
B. 矩阵转置
Problem ID: 8157
Contest ID: 5321
必做题
Compile Error
时间限制: 1000ms
空间限制: 512000kB
题目描述
输入一个nm矩阵,请输出它的转置矩阵,即把行列互换,输出mn的矩阵。
输入格式
第一行两个整数n,m。
下面有n行,每行有m个整数。
输出格式
转置矩阵m*n。
样例
Input 1
5 4 3 3 3 4 2 0 0 3 0 3 1 4 3 4 3 3 1 0 3 3
Output 1
3 2 0 3 1 3 0 3 4 0 3 0 1 3 3 4 3 4 3 3
数据范围
1<=n, m<=500
样例解释
输入一个5行4列的矩阵,输出一个4行5列的矩阵。
2 个赞
代码格式化一下
1 个赞
#include <iostream>
#include <string.h>
using namespace std;
void In(int(*str)[N],int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("&d",&str[i][j]);
}
}
}
void Up(int(*str)[N],int n){
int temp=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j){
continue;
}else if(i<j){
temp=str[i][j];
str[i][j]=str[j][i];
str[j][i]=temp;
}
}
}
}
void Out(int(*str)[N],int n){
int temp=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(j==0){
cout<<str[i][j];
}else{
cout<<" "<<str[i][j];
}
}
cout<<endl;
}
}
int main() {
int n;
while(~scanf("%d",&n)){
if(n<1||n>100){
break;
}
int str[N][N];
In(str,n);
Up(str,n);
Out(str,n);
}
}
试亿下,大概行
[spoiler]不确定