Files
python/TangDou/LuoGuBook/第8章模拟与高精度/P4924_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

50 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}