1、折点计数
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int a[N];
int main() {
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> a[i];
int res = 0;
for (int i = 1; i < n - 1; i ++ )
if (a[i] > a[i - 1] && a[i] > a[i + 1] || a[i] < a[i - 1] && a[i] < a[i + 1])
res ++ ;
cout << res << endl;
return 0;
}
2、俄罗斯方块
对于每一个掉落图形,依次枚举它掉落过程中第一次和原图形发生重叠的时刻。如果该时刻 t t t 发生重叠,那么就应该输出 t − 1 t-1 t−1 时刻的图形。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 20;
int g[N][N], s[N][N]; // 原始画布,新画布
int p[4][4]; // 掉落的图形
bool draw(int x, int y) {
// 左上角坐标
memcpy(s, g, sizeof s);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (p[i][j]) {
int a = x + i, b = y + j;
s[a][b]++;
if (s[a][b] == 2) return true