'commit'
This commit is contained in:
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_1.cpp
Normal file
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_1.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], f[N][N];
|
||||
|
||||
//矩阵旋转,需要额外开创建一个临时的二维数组(防止出现交换赋值时,出现覆盖的问题)
|
||||
void rotate(int x, int y, int r) { // x:第几行 y:以(x,y)点为中心,四个方向,向左r,向右r,转~
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++) //
|
||||
f[x - j][y + i] = res[x + i][y + j]; //副本修改
|
||||
|
||||
// 复制数组的正规写法
|
||||
memcpy(res, f, sizeof f); //将副本复制回到正本上
|
||||
|
||||
//复制
|
||||
// for (int i = 1; i <= n; i++)
|
||||
// for (int j = 1; j <= n; j++)
|
||||
// res[i][j] = f[i][j];
|
||||
}
|
||||
|
||||
int main() {
|
||||
//初始化填充数据
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
res[i][j] = f[i][j] = ++cnt;
|
||||
|
||||
//输出结果
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%02d ", 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("%02d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user