题目描述
首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。 操作类型有四种: 1 2 表示:90度,顺时针,翻转4个数 1 3 表示:90度,顺时针,翻转9个数 2 2 表示:90度,逆时针,翻转4个数 2 3 表示:90度,逆时针,翻转9个数
输入描述:
输入有多组数据。
每组输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。
输出描述:
输出翻转后的数组。
示例1
输入
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
1 3 1 1
输出
11 6 1 4 5
12 7 2 9 10
13 8 3 14 15
16 17 18 19 20
21 22 23 24 25
题目解析:还是两种方法,一种暴力求解,一种找规律(不过我没写,思想写在了代码中)
代码:
#include <iostream>
#include <algorithm>
using namespace std;
//暴力破解
//也可以:顺时针的话,先将矩阵上下翻转,再按对角线交换
// 逆时针的话,先将矩阵上下翻转,再按反对角线交换
void ri90(int tmp[5][5], int flag, int x, int y)
{
if(flag==2)
{
swap(tmp[x][y],tmp[x][y+1]);
swap(tmp[x+1][y],tmp[x+1][y+1]);
swap(tmp[x][y],tmp[x+1][y+1]);
}
else
{
swap(tmp[x][y],tmp[x][y+2]);
swap(tmp[x+2][y],tmp[x+2][y+2]);
swap(tmp[x][y],tmp[x+2][y+2]);
swap(tmp[x][y+1],tmp[x+1][y+2]);
swap(tmp[x+1][y],tmp[x+2][y+1]);
swap(tmp[x][y+1],tmp[x+2][y+1]);
}
}
void le90(int tmp[5][5], int flag, int x, int y)
{
if(flag==2)
{
swap(tmp[x][y],tmp[x][y+1]);
swap(tmp[x+1][y],tmp[x+1][y+1]);
swap(tmp[x][y+1],tmp[x+1][y]);
}
else
{
swap(tmp[x][y],tmp[x][y+2]);
swap(tmp[x+2][y],tmp[x+2][y+2]);
swap(tmp[x][y+2],tmp[x+2][y]);
swap(tmp[x][y+1],tmp[x+1][y+2]);
swap(tmp[x+1][y],tmp[x+2][y+1]);
swap(tmp[x+1][y],tmp[x+1][y+2]);
}
}
int main()
{
int tmp[5][5],i,j;
for(i=0;i<5;++i)
for(j=0;j<5;++j)
cin >> tmp[i][j];
int m,n,x,y;
cin>>m>>n>>x>>y;
--x;
--y;
if(m==1)
ri90(tmp,n,x,y);
else le90(tmp,n,x,y);
for(i=0;i<5;++i)
for(j=0;j<5;++j)
{
cout<<tmp[i][j];
if(j==4)
cout<<endl;
else cout<<" ";
}
return 0;
}