题目描述
首先输入一个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
分析
这题很容易受到之前判断矩阵旋转角的影响,看起来很像,不同之处在于,首先这个肯定是旋转2列或3列的,其次这个是在原来的基础上判断旋转后当前位置的数是没转之前哪个位置的数,而不是当前位置的数旋转后去了哪个位置
最多9个数,并不多,在纸上写一些,记住用相对坐标来表示位置,也就是用[x,y],[x+1,y]等来表示坐标,而不是具体的数,列出旋转前后的位置,很容易就能找到规律
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int mat[10][10],mat2[10][10];
int main()
{
while (cin>>mat[1][1])
{
for (int j = 2; j <= 5; j++)scanf("%d", &mat[1][j]);
for (int i = 2; i <= 5; i++)
for (int j = 1; j <= 5; j++)
scanf("%d", &mat[i][j]);
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
mat2[i][j] = mat[i][j];
int o1, o2, x, y;
cin >> o1 >> o2 >> x >> y;
if (o1 == 1 && o2 == 2 && x+1<=5 && y+1<=5)
{
mat2[x][y+1] = mat[x][y];
mat2[x+1][y + 1] = mat[x][y + 1];
mat2[x][y] = mat[x + 1][y];
mat2[x + 1][y] = mat[x + 1][y + 1];
}
else if (o1 == 2 && o2 == 2 && x+1<=5 && y+1 <=5)
{
mat2[x+1][y] = mat[x][y]; mat2[x + 1][y+1] = mat[x+1][y]; mat2[x][y] = mat[x][y+1]; mat2[x][y + 1] = mat[x+1][y+1];
}
else if (o1 == 1 && o2 == 3)
{
for(int i =0;i<3 && x+i<=5;i++)
for (int j = 0; j < 3 && y+j<=5; j++)
{
mat2[x+i][y+j] = mat[x+2-j][y+i];
}
}
else if (o1 == 2 && o2 == 3)
{
for (int i = 0; i<3 && x + i <= 5; i++)
for (int j = 0; j < 3 && y + j <= 5; j++)
{
//mat2[x + 3 - (y + j)][x+i]=mat[x + i][y + j] ;
mat2[x + i][y + j] = mat[x+j][y+2-i];
}
}
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
cout << mat2[i][j] << " ";
}
cout << "\n";
}
}
}