11023: 【原1023】矩阵翻转
题目
题目描述
author: ghostgold 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1023
Description
给定一个正方形的整数矩阵,输出将该矩阵按某一方向翻转后的结果。
Input Format
输入第一行有一个整数n,表示一共有n组数据;n不会为负数。
之后有n组数据,对于每组数据:
第一行有两个整数a和b,分别表示正方形矩阵的边长,以及翻转的方向。
当b=0时水平翻转,当b=1时竖直翻转,当b=2时以主对角线为轴翻转。
b不会取其他值。
Output Format
输出共有n组,分别对应n组输入,输出相应矩阵翻转后的结果(仍是一个矩阵)。
相邻矩阵、相邻行之间没有空行,一行中相邻两个数字之间有且仅有一个空格。
Sample Input
2
2 0
-2 4
8 -16
3 2
1 2 3
4 5 6
7 8 9
Sample Output
4 -2
-16 8
1 4 7
2 5 8
3 6 9
Limits
对于30%的数据,\( n \leq 100 \);
对于100%的数据,\(n \leq 1000 \)。
对于100%的数据,矩阵的边长\(a \leq 600\)。
FineArtz's solution
/* 矩阵反转 */
#include <iostream>
using namespace std;
int mat[605][605] = {0};
void trans(const int &n, const int &k){
switch(k){
case 0:
for (int i = 1; i <= n; ++i){
for (int j = n; j >= 1; --j)
cout << mat[i][j] << ' ';
cout << "\n";
}
break;
case 1:
for (int i = n; i >= 1; --i){
for (int j = 1; j <= n; ++j)
cout << mat[i][j] << ' ';
cout << "\n";
}
break;
case 2:
for (int j = 1; j <= n; ++j){
for (int i = 1; i <= n; ++i)
cout << mat[i][j] << ' ';
cout << "\n";
}
break;
default:
break;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
while (n--){
int a, b;
cin >> a >> b;
for (int i = 1; i <= a; ++i)
for (int j = 1; j <= a; ++j)
cin >> mat[i][j];
trans(a, b);
}
return 0;
}
ligongzzz's solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (; n > 0; --n) {
int a, b;
cin >> a >> b;
vector<vector<int>> mat(a,vector<int>(a,0));
if (b == 0) {
for (int i = 0; i < a; ++i) {
for (int j = 0; j < a; ++j) {
cin >> mat[i][a - j - 1];
}
}
}
else if (b == 1) {
for (int i = 0; i < a; ++i) {
for (int j = 0; j < a; ++j) {
cin >> mat[a - i - 1][j];
}
}
}
else {
for (int i = 0; i < a; ++i) {
for (int j = 0; j < a; ++j) {
cin >> mat[j][i];
}
}
}
for (auto& p : mat) {
for (auto q : p) {
cout << q << " ";
}
cout << "\n";
}
}
return 0;
}
victrid's solution
#include <iostream>
using namespace std;
void output(int***,int,int,int);
int main(){
int totalMatricesCount;
cin >> totalMatricesCount;
int ***totalMatricesPointer=new int**[totalMatricesCount];
int *MatrixFloorPointer=new int[totalMatricesCount];
int *MatrixRotatePointer=new int[totalMatricesCount];
for(int Matrixnum=0;Matrixnum<totalMatricesCount;Matrixnum++){
cin >> *(MatrixFloorPointer+Matrixnum);
cin >> *(MatrixRotatePointer+Matrixnum);
*(totalMatricesPointer+Matrixnum)=new int *[*(MatrixFloorPointer+Matrixnum)];
for(int row=0;row<*(MatrixFloorPointer+Matrixnum);row++)
*(*(totalMatricesPointer+Matrixnum)+row)=new int [*(MatrixFloorPointer+Matrixnum)];
for(int row=0;row<*(MatrixFloorPointer+Matrixnum);row++)
for(int column=0;column<*(MatrixFloorPointer+Matrixnum);column++)
cin >>*(*(*(totalMatricesPointer+Matrixnum)+row)+column);
}
for(int Matrixnum=0;Matrixnum<totalMatricesCount;Matrixnum++){
output(totalMatricesPointer+Matrixnum,*(MatrixFloorPointer+Matrixnum),*(MatrixRotatePointer+Matrixnum),Matrixnum);
}
return 0;
}
void output(int*** totalMatricesPointer,int floor,int rotate,int NOT_FIRST_MATIRX_FLAG){
if(NOT_FIRST_MATIRX_FLAG)cout << endl;
bool NOT_FIRST_ROW_FLAG=false;
bool NOT_FIRST_COLUMN_FLAG=false;
for(int row=0;row<floor;row++){
if(NOT_FIRST_ROW_FLAG)cout << endl;
NOT_FIRST_ROW_FLAG = true;
for(int column=0;column<floor;column++){
if(NOT_FIRST_COLUMN_FLAG)cout << ' ';
NOT_FIRST_COLUMN_FLAG = true;
if(rotate==2)cout << *(*(*(totalMatricesPointer)+column)+row);
if(rotate==1)cout << *(*(*(totalMatricesPointer)+(floor-row-1))+column);
if(rotate==0)cout << *(*(*(totalMatricesPointer)+row)+(floor-column-1));
}
NOT_FIRST_COLUMN_FLAG = false;
}
return;
}
yyong119's solution
#include <iostream>
#include <cstring>
int s[601][601],c[601][601];
int n,i,j,k,m;
int main(){
using namespace std;
cin>>n;
while (n){
cin>>m>>k; memset(s,0,sizeof(s)); memset(c,0,sizeof(c));
for (i=1; i<=m; i++) for (j=1; j<=m; j++) cin>>s[i][j];
if (k==0){
for (i=1; i<=m; i++)
for (j=1; j<=m; j++) c[i][j]=s[i][m-j+1];
}else
if (k==1){
for (i=1; i<=m; i++)
for (j=1; j<=m; j++) c[i][j]=s[m-i+1][j];
}else
if (k==2){
for (i=1; i<=m; i++)
for (j=1; j<=m; j++) c[i][j]=s[j][i];
}
for (i=1; i<=m; i++){
for (j=1; j<=m-1; j++) cout<<c[i][j]<<" ";
cout<<c[i][m]<<endl;
}
n--;
}
return 0;
}