'commit'
This commit is contained in:
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_2.cpp
Normal file
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_2.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Rotate Image(二维数组顺时针旋转90度)
|
||||
const int N = 110;
|
||||
int n = 9, cnt;
|
||||
|
||||
int res[N][N], tmp[N][N];
|
||||
|
||||
//矩阵旋转,需要额外开创建一个临时的二维数组(防止出现交换赋值时,出现覆盖的问题)
|
||||
void rotate(int x, int y, int r) {
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++)
|
||||
//每一个位置都去了哪里
|
||||
tmp[x + j][y - i] = res[x + i][y + j];
|
||||
|
||||
//复制回来
|
||||
memcpy(res, tmp, sizeof tmp);
|
||||
// for (int i = 1; i <= n; i++)
|
||||
// for (int j = 1; j <= n; j++)
|
||||
// res[i][j] = tmp[i][j];
|
||||
}
|
||||
|
||||
int main() {
|
||||
//初始化数据
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
res[i][j] = tmp[i][j] = ++cnt;
|
||||
|
||||
//输出结果
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%2d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
//测试一下旋转
|
||||
rotate(6, 7, 1);
|
||||
|
||||
//输出结果
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%2d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user