输入有多组数据。 每组输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。
输出翻转后的数组。
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
将矩阵旋转的通用方法:
1.顺时针旋转:先将矩阵上下翻转,再将矩阵元素沿对角线对称交换
2.逆时针旋转:先将矩阵左右翻转,再将矩阵元素沿对角线对称交换
#include <iostream>
#include <algorithm>
using namespace std;
// 上下翻转
void flipTop2Down(int(&mat)[5][5], int x, int y, int len) {
int down = x + len - 1;
while (down > x) {
for (int i = y; i < y + len; ++i)
swap(mat[x][i], mat[down][i]);
++x, --down;
}
}
// 左右翻转
void flipLeft2Right(int(&mat)[5][5], int x, int y, int len) {
int right = y + len - 1;
while (y < right) {
for (int i = x; i < x + len; ++i)
swap(mat[i][y], mat[i][right]);
++y, --right;
}
}
// 对称
void sym(int(&mat)[5][5], int x, int y, int len) {
for (int i = 0; i < len; ++i) {
for (int j = i + 1; j < len; ++j) {
swap(mat[x+i][y+j], mat[x+j][y+i]);
}
}
}
void print(int(&mat)[5][5]) {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
cout << mat[i][j] << ' ';
}
cout << endl;
}
}
int main() {
int mat[5][5];
int direct, len, x, y;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
cin >> mat[i][j];
}
}
cin >> direct >> len >> x >> y;
x = x - 1;
y = y - 1;
// 顺时针旋转
if (direct == 1) {
// 上下翻转
flipTop2Down(mat, x, y, len);
// 对称交换
sym(mat, x, y, len);
}
else {
// 左右翻转
flipLeft2Right(mat, x, y, len);
// 对称交换
sym(mat, x, y, len);
}
print(mat);
}
package com.speical.first;
import java.util.Scanner;
/**
* C翻转
*
* 很简单,模拟正常翻转就可以了
*
* 我们可以发现当顺时针90的时候
* 第一行的数据依次为第一列的倒序
* 第二行的数据依次为第二列的倒序
* ....
*
* 我们可以发现当逆时针90的时候
* 第一行的数据依次为最后一列的正序
* 第二行的数据依次为倒数第二列的正序
* ....
*
* So, it it easy that you can handle it!
* @author special
* @date 2018年2月3日 下午4:18:16
*/
public class Pro181 {
static int[][] nums = new int[5][5];
static int[][] auxiliary = new int[5][5];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()){
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
nums[i][j] = input.nextInt();
auxiliary[i][j] = nums[i][j];
}
}
int kind = input.nextInt();
int count = input.nextInt();
int startX = input.nextInt() - 1; //数组的索引从0开始,所以此处减一
int startY = input.nextInt() - 1;
int endX = startX + count - 1;
int endY = startY + count - 1;
//旋转
for(int i = startX; i <= endX; i++){
for(int j = startY; j <= endY; j++){
if(kind == 1){
nums[i][j] = auxiliary[endX - (j - startY)][startY + (i - startX)];
}else if(kind == 2){
nums[i][j] = auxiliary[startX + (j - startY)][endY - (i - startX)];
}
}
}
//输出数组
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
System.out.print((j == 0 ? "" : " ") + nums[i][j]);
}
System.out.println();
}
}
}
}
#include<iostream> #include<stdio.h> #include<string> using namespace std; int main() { string str1, str2; cin >> str1; getchar(); //这一步很重要 while (getline(cin,str2)) { int i = 0, j = 0; for (;i+j < str2.size();) { if ((str2[i + j]|32) == (str1[j]|32)) j++; else { i++; j = 0; } if (j == str1.size()) { for (int z = i;z < i + str1.size();++z) str2[z] = ' '; i = i + str1.size(); j = 0; } } for (int i = 0;i < str2.size();++i) if (str2[i] == ' ') continue; else cout << str2[i]; cout << endl; } return 0; }
#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;
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int[][] arr = new int[5][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { arr[i][j] = scanner.nextInt(); } } int a = scanner.nextInt(); int b = scanner.nextInt(); //对应下标为x-1,y-1 int x = scanner.nextInt(); int y = scanner.nextInt(); //获取以x,y为左上角的b²个数 int[][] arrBefore = new int[b][b]; for (int i = 0; i < b; i++) { for (int j = 0; j < b; j++) { arrBefore[i][j] = arr[i+x-1][j+y-1]; } } //将这些数按要求旋转 int[][] arrFlip = flip(arrBefore, a, b); for (int i = 0; i < arrFlip.length; i++) { for (int j = 0; j < arrFlip.length; j++) { arr[i+x-1][j+y-1] = arrFlip[i][j]; } } //输出旋转后的数组 for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } } } public static int[][] flip(int[][] arrBefore,int a,int b) { int[][] arrFlip = new int[b][b]; if (a == 1) { //顺时针翻转90度 for (int i = 0; i < b; i++) { for (int j = 0; j < b; j++) { arrFlip[i][j] = arrBefore[b-j-1][i]; } } return arrFlip; }else if (a == 2) { //逆时针翻转90度 for (int i = 0; i < b; i++) { for (int j = 0; j < b; j++) { arrFlip[i][j] = arrBefore[j][b-i-1]; } } return arrFlip; } return arrFlip; } }
#include <array> #include <iostream> #include <istream> #include <ostream> using namespace std; using Matrix = array<array<int, 6>, 6>; istream& operator>>(istream& in, Matrix& m) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { in >> m[i][j]; } } return in; } ostream& operator<<(ostream& out, const Matrix& m) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { out << m[i][j] << " "; } out << endl; } return out; } void turn(Matrix& m, int direction, int number, int x, int y) { if (direction == 1 && number == 2) { //90度,顺时针,翻转4个数 int temp = m[x][y]; m[x][y] = m[x + 1][y]; m[x + 1][y] = m[x + 1][y + 1]; m[x + 1][y + 1] = m[x][y + 1]; m[x][y + 1] = temp; } else if (direction == 1 && number == 3) { //90度,顺时针,翻转9个数 int temp = m[x][y]; m[x][y] = m[x + 2][y]; m[x + 2][y] = m[x + 2][y + 2]; m[x + 2][y + 2] = m[x][y + 2]; m[x][y + 2] = temp; temp = m[x + 1][y]; m[x + 1][y] = m[x + 2][y + 1]; m[x + 2][y + 1] = m[x + 1][y + 2]; m[x + 1][y + 2] = m[x][y + 1]; m[x][y + 1] = temp; } else if (direction == 2 && number == 2) { //90度,逆时针,翻转4个数 int temp = m[x][y]; m[x][y] = m[x][y + 1]; m[x][y + 1] = m[x + 1][y + 1]; m[x + 1][y + 1] = m[x + 1][y]; m[x + 1][y] = temp; } else { //direction == 2 && number == 3(90度,逆时针,翻转9个数) int temp = m[x][y]; m[x][y] = m[x][y + 2]; m[x][y + 2] = m[x + 2][y + 2]; m[x + 2][y + 2] = m[x + 2][y]; m[x + 2][y] = temp; temp = m[x + 1][y]; m[x + 1][y] = m[x][y + 1]; m[x][y + 1] = m[x + 1][y + 2]; m[x + 1][y + 2] = m[x + 2][y + 1]; m[x + 2][y + 1] = temp; } } int main() { Matrix m; int direction, number, x, y; while (cin >> m >> direction >> number >> x >> y) { turn(m, direction, number, x, y); cout << m; } return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; void turn(vector<vector<int>>& vv, int a, int b, int i, int j){ if (b == 2){ swap(vv[i][j], vv[i][j + 1]); swap(vv[i + 1][j], vv[i + 1][j + 1]); if (a == 1) swap(vv[i][j], vv[i + 1][j + 1]); else swap(vv[i][j + 1], vv[i + 1][j]); }else{ swap(vv[i][j], vv[i][j + 2]); swap(vv[i + 2][j], vv[i + 2][j + 2]); if (a == 1) swap(vv[i][j], vv[i + 2][j + 2]); else swap(vv[i][j + 2], vv[i + 2][j]); swap(vv[i][j + 1], vv[i + 1][j + 2]); swap(vv[i + 1][j], vv[i + 2][j + 1]); if (a == 1) swap(vv[i][j + 1], vv[i + 2][j + 1]); else swap(vv[i + 1][j + 2], vv[i + 1][j]); } } int main() { vector<vector<int>> vv(5, vector<int>(5)); int a, b, i, j; while (cin >> vv[0][0] >> vv[0][1] >> vv[0][2] >> vv[0][3] >> vv[0][4]){ for (int i = 1; i <= 4; ++i) for (int j = 0; j <= 4; ++j) cin >> vv[i][j]; cin >> a >> b >> i >> j; turn(vv, a, b, i - 1, j - 1); for (auto &v : vv){ for (int &n : v) cout << n << " "; cout << endl; } } }
#include<iostream> #include<vector> #include<algorithm> using namespace std; //矩阵阶数 int n = 5; struct Instruction{ vector<int> op; pair<int, int> ad; }; void input(vector<vector<int>>& Matrix) { int num = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cin >> num; Matrix[i][j] = num; } } } // void input(Instruction& I) { int op1, op2; cin >> op1 >> op2; I.op.push_back(op1); I.op.push_back(op2); cin >> I.ad.first >> I.ad.second; } //顺时针旋转二维的[(x, y), [px, py]] void ROTATE(vector<vector<int>>& Matrix, int x, int y, int px, int py) { int n = px - x; vector<vector<int>> ans(Matrix); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { ans[j + x][n - 1 - i + y] = Matrix[i + x][j + y]; } } Matrix = ans; } //逆时针等于转三次顺时针 void anti_clockWise(vector<vector<int>>& Matrix, int x, int y, int px, int py) { for(int i = 0; i < 3; i++) { ROTATE(Matrix, x, y, px, py); } } //执行指令 void Excute(Instruction& I, vector<vector<int>>& Matrix) { auto [x, y] = I.ad; x--; y--; auto px = x + I.op.at(1), py = y + I.op.at(1); if(I.op.at(0) == 1) { ROTATE(Matrix, x, y, px, py); }else{ anti_clockWise(Matrix, x, y, px, py); } } //打印 void printMatrix(const vector<vector<int>>& Matrix) { for(auto row : Matrix) { for(auto num : row) { cout << num << " "; } cout << endl; } } int main() { //INIT Instruction I; vector<vector<int>> Matrix(n, vector<int>(n)); input(Matrix); input(I); Excute(I, Matrix); printMatrix(Matrix); }
#include<iostream> using namespace std; int main() { int d[5][5],od[5][5],t1,t2,x,y; while(cin >> d[0][0] >> d[0][1] >> d[0][2] >> d[0][3] >> d[0][4]) { for(int i = 0;i < 5;i++) od[0][i] = d[0][i]; for(int i = 1;i < 5;i++) { for(int j = 0;j < 5;j++) { cin >> d[i][j]; od[i][j] = d[i][j]; } } cin >> t1 >> t2 >> x >> y; if(t1 == 1 && t2 == 2) { for(int i = x - 1,m = y - 1;i < x + 1,m < y + 1;i++,m++) { for(int j = y - 1,n = x;j < y + 1,n >= x - 1;j++,n--) { od[i][j] = d[n][m]; } } } else if(t1 == 1 && t2 == 3) { for(int i = x - 1,m = y - 1;i < x + 2,m < y + 2;i++,m++) { for(int j = y - 1,n = x + 1;j < y + 2,n >= x - 1;j++,n--) { od[i][j] = d[n][m]; } } } else if(t1 == 2 && t2 == 2) { for(int i = x - 1,m = y;i < x + 1,m >= y - 1;i++,m--) { for(int j = y - 1,n = x - 1;j < y + 1,n < x + 1;j++,n++) { od[i][j] = d[n][m]; } } } else if(t1 == 2 && t2 == 3) { for(int i = x - 1,m = y + 1;i < x + 2,m >= y - 1;i++,m--) { for(int j = y - 1,n = x - 1;j < y + 2,n < x + 2;j++,n++) { od[i][j] = d[n][m]; } } } for(int i = 0;i < 5;i++) { for(int j = 0;j < 5;j++) { cout << od[i][j] << " "; } cout << endl; } } return 0; }
#include<stdio.h> #include<iostream> #include<cstdio> using namespace std; int buf[5][5]; int helper[5][5]; void rotateRight(int matrix[5][5], int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int tmp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = tmp; } } for (int i = 0; i < n; i++) { int left = 0, right = n - 1; while (left < right) { int tmp = matrix[i][left]; matrix[i][left] = matrix[i][right]; matrix[i][right] = tmp; left++; right--; } } } void rotateLeft(int matrix[5][5], int n) { rotateRight(matrix, n); rotateRight(matrix, n); rotateRight(matrix, n); } void printMatrix(int buf[5][5]) { for (int i = 0; i < 5; i++) { cout << buf[i][0] << " "<< buf[i][1] << " " << buf[i][2] << " " << buf[i][3] << " " << buf[i][4] << endl; } } void solve(int matrix[5][5], int a, int b, int row, int col) { for (int i = row; i < row + b; i++) { for (int j = col; j < col + b; j++) { helper[i - row][j - col] = matrix[i][j]; } } if (a == 1) rotateRight(helper, b); else rotateLeft(helper, b); for (int i = row; i < row + b; i++) { for (int j = col; j < col + b; j++) { matrix[i][j] = helper[i - row][j - col]; } } } int main() { int n; int a, b, row, col; while (scanf("%d", &n) != EOF) { buf[0][0] = n; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 0 && j == 0) continue; cin >> buf[i][j]; } } cin >> a >> b >> row >> col; solve(buf, a, b, row - 1, col - 1); printMatrix(buf); } return 0; }为了避免思路混乱,采用 helper 将需要进行旋转的子矩阵块提取出来,旋转好后再放回去。
#include<cstdio> #include<algorithm> int v[6][6]; int main(){ int n1,n2,x,y; while(scanf("%d",&v[1][1])!=EOF){ for(int i=1;i<=5;++i){ for(int j=1;j<=5;++j){ if(i!=1||j!=1) scanf("%d",&v[i][j]); } } scanf("%d %d %d %d",&n1,&n2,&x,&y); for(int i=1;i<=5;++i){ for(int j=1;j<=5;++j){ if(i>=x&&i<x+n2&&j>=y&&j<y+n2){ int X1=i-x+1,Y1=j-y+1; if(n1==1) { if(j==1) printf("%d",v[x+n2-Y1][y+X1-1]); else printf(" %d",v[x+n2-Y1][y+X1-1]); } else{ if(j==1) printf("%d",v[x+Y1-1][y+n2-X1]); else printf(" %d",v[x+Y1-1][y+n2-X1]); } }else { if(j==1) printf("%d",v[i][j]); else printf(" %d",v[i][j]); } } printf("\n"); } } return 0; }
//用矩阵乘法实现的矩阵旋转,要思考的地方比较多,感觉暴力写可能更快吧 //要实现矩阵顺时针旋转90度,只要把矩阵转置再右乘一个单位矩阵的变形阵即可 //例如矩阵 1 2 要变成 3 1,可以先转置成 1 3再右乘 0 1即可 // 3 4 4 2 2 4 1 0 //要逆时针旋转90度只要左乘那个变形的单位阵即可 //左行右列原理 #include<iostream> (720)#include<cstdio> #include<algorithm> (831)#include<cstring> using namespace std; const int MAXN = 6; //写矩阵算法要特别注意i和j,千万不要错写!!! //这题矩阵旋转我是按照矩阵乘法算的,思路正确 struct Matrix { int row, col; int matrix[MAXN][MAXN]; Matrix(int x,int y):row(x),col(y){ memset(matrix, 0, sizeof(matrix)); } Matrix() { memset(matrix, 0, sizeof(matrix)); } }; Matrix tansposition(Matrix a) { Matrix answer = a; for (int i = 0; i < a.row; i++) { for (int j = 0; j < a.col; j++) { answer.matrix[j][i] = a.matrix[i][j]; } } return answer; } Matrix ClockWise90Degree(int n) { Matrix answer(n, n); for (int i = 0; i < answer.row; i++) { for (int j = 0; j < answer.col; j++) { if (i + j == n - 1) { answer.matrix[i][j] = 1; } else { answer.matrix[i][j] = 0; } } } return answer; } //逆时针的矩阵似乎没必要,顺时针矩阵右乘欲旋转的矩阵即可 Matrix AntiClockWise90Degree(int n) { Matrix answer(n, n); for (int i = 0; i < answer.row; i++) { for (int j = 0; j < answer.col; j++) { if (i + j == n - 1) { answer.matrix[i][j] = 1; } else { answer.matrix[i][j] = 0; } } } return answer; } //默认a,b均为方阵 Matrix Multiple(Matrix a, Matrix b) { Matrix answer(a.row, b.col); memset(answer.matrix, 0, sizeof(answer.matrix)); for (int i = 0; i < a.row; i++) { for (int j = 0; j < b.col; j++) { for (int k = 0; k < a.col; k++) { answer.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j]; //cout<< a.matrix[i][k] <<endl; //cout<< b.matrix[k][j] <<endl; //cout<< answer.matrix[i][j] <<endl; } } } return answer; } //输出矩阵的算法 void print(Matrix a) { for (int i = 0; i < a.row; i++) { for (int j = 0; j < a.col; j++) { if (j == 0) { printf("%d", a.matrix[i][j]); } else { printf(" %d", a.matrix[i][j]); } } printf("\n"); } return; } int main() { Matrix res(5, 5); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { scanf("%d", &res.matrix[i][j]); } } int a, b, x, y; Matrix temp; scanf("%d%d%d%d", &a, &b, &x, &y); //题目默认矩阵行,列均从1开始 x--; y--; if (b == 2) { temp = ClockWise90Degree(2); Matrix t(2, 2); for (int i = x; i < x + 2; i++) { for (int j = y; j < y + 2; j++) { t.matrix[i - x][j - y] = res.matrix[i][j]; } } t = tansposition(t); //print(temp); //print(t); if (a == 1) { temp = Multiple(t, temp); for (int i = x; i < x + 2; i++) { for (int j = y; j < y + 2; j++) { res.matrix[i][j] = temp.matrix[i - x][j - y]; } } } else { temp = Multiple(temp, t); for (int i = x; i < x + 2; i++) { for (int j = y; j < y + 2; j++) { res.matrix[i][j] = temp.matrix[i - x][j - y]; } } } } else { temp = ClockWise90Degree(3); Matrix t(3, 3); for (int i = x; i < x + 3; i++) { for (int j = y; j < y + 3; j++) { t.matrix[i - x][j - y] = res.matrix[i][j]; } } t = tansposition(t); if (a == 1) { temp = Multiple(t, temp); for (int i = x; i < x + 3; i++) { for (int j = y; j < y + 3; j++) { res.matrix[i][j] = temp.matrix[i - x][j - y]; } } } else { temp = Multiple(temp, t); for (int i = x; i < x + 3; i++) { for (int j = y; j < y + 3; j++) { res.matrix[i][j] = temp.matrix[i - x][j - y]; } } } } print(res); return 0; }
while True: try: c = [] for _ in range(5): c.append(list(input().split())) b = list(map(int,input().split())) x = b[2] - 1 y = b[3] - 1 if b[0] == 1 and b[1] == 2: c[x][y],c[x][y+1],c[x+1][y+1],c[x+1][y] = c[x+1][y],c[x][y],c[x][y+1],c[x+1][y+1] elif b[0] == 1 and b[1] == 3: c[x][y],c[x][y+1],c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1],c[x+2][y],c[x+1][y]\ = c[x+2][y],c[x+1][y],c[x][y],c[x][y+1],c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1] elif b[0] == 2 and b[1] == 2: c[x][y],c[x][y+1],c[x+1][y+1],c[x+1][y] = c[x][y+1],c[x+1][y+1],c[x+1][y],c[x][y] else: c[x][y],c[x][y+1],c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1],c[x+2][y],c[x+1][y]\ = c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1],c[x+2][y],c[x+1][y],c[x][y],c[x][y+1] for i in c: print(' '.join(map(str,i))) except: break
#include<iostream> (720)#include<vector> using namespace std; int main() { vector<vector<int>> martix(5); for (int i = 0; i < 5; i++) martix[i].resize(5); for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) cin >> martix[i][j]; vector<vector<int>> temp = martix; int a, b, x, y; cin >> a >> b >> x >> y; y--; x--; if (a == 1) for (int i = x; i < x + b; i++) for (int j = y; j < y + b; j++) martix[j - y + x][y + b - 1 - i + x] = temp[i][j]; else for (int i = x; i < x + b; i++) for (int j = y; j < y + b; j++) martix[b - j + y + x - 1][i - x + y] = temp[i][j]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << martix[i][j] << " "; } cout << endl; } }
#include<iostream> (720)#include<cstring> using namespace std; int tmp[3][3]; void rotate(int a[][5],int n,int x,int y){ for(int i =0;i<n;i++) for(int j=0;j<n;j++) tmp[j][n-i-1] = a[i+x-1][j+y-1]; for(int i =0;i<n;i++) for(int j=0;j<n;j++) a[i+x-1][j+y-1] = tmp[i][j]; } int main(){ int matrix[5][5]; int a[4]; for(int i =0;i<5;i++) for(int j=0;j<5;j++) cin>>matrix[i][j]; for(int i = 0;i<4;i++) cin>>a[i]; if(a[0]==1&&a[1]==2){ rotate(matrix,2,a[2],a[3]); } if(a[0]==1&&a[1]==3){ rotate(matrix,3,a[2],a[3]); } if(a[0]==2&&a[1]==2){ rotate(matrix,2,a[2],a[3]); rotate(matrix,2,a[2],a[3]); rotate(matrix,2,a[2],a[3]); } if(a[0]==2&&a[1]==3){ rotate(matrix,3,a[2],a[3]); rotate(matrix,3,a[2],a[3]); rotate(matrix,3,a[2],a[3]); } for(int i =0;i<5;i++){ for(int j=0;j<5;j++) cout<<matrix[i][j]<<" "; cout<<endl; } }
#include<iostream> using namespace std; int a[5][5]; int res[5][5]; typedef struct vec{ double x,y; vec(){ x=0;y=0; } vec(double a,double b):x(a),y(b){ } vec operator-(const vec& v){ return vec(x-v.x,y-v.y); } vec operator+(const vec& v){ return vec(x+v.x,y+v.y); } vec rot(int op){ if(op==1) return vec(-y,x); else return vec(y,-x); } }vec; int main(){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ cin>>a[i][j]; res[i][j]=a[i][j]; } } int o1,o2,x,y; cin>>o1>>o2>>x>>y; for(int i=x-1;i<x-1+o2;i++){ for(int j=y-1;j<y-1+o2;j++){ vec O; O.x=((double)o2-1)/2+y-1; O.y=((double)o2-1)/2+x-1; vec des=(vec(j,i)-O).rot(o1)+O; res[(int)des.y][(int)des.x]=a[i][j]; } } for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ cout<<res[i][j]<<' '; } cout<<endl; } return 0; }利用线性代数的知识,可以直接用旋转矩阵乘以坐标得到结果
def one(x,y,index): list1=[] for j in range(y-1,y-1+index): re=[] for i in range(x-1,x-1+index): re.append(arr[i][j]) list1.append(re[::-1]) for i in range(x-1,x-1+index): for j in range(y-1,y-1+index): arr[i][j]=list1[i-x+1][j-y+1] def two(x,y,index): list1=[] for j in range(y-2+index,y-2,-1): re=[] for i in range(x-1,x-1+index): re.append(arr[i][j]) list1.append(re) for i in range(x-1,x-1+index): for j in range(y-1,y-1+index): arr[i][j]=list1[i-x+1][j-y+1] while True: try: arr=[] for i in range(5): arr.append(list(input().strip().split(' '))) inp=list(map(int,input().strip().split(' '))) if [inp[0],inp[1]]==[1,2]: one(inp[2],inp[3],2) elif [inp[0],inp[1]]==[1,3]: one(inp[2],inp[3],3) elif [inp[0],inp[1]]==[2,2]: two(inp[2],inp[3],2) else: two(inp[2],inp[3],3) for i in arr: print(' '.join(i)) except: break