题:3227. 折点计数
5 4 1 2 3 6 4
思路:
(a[i - 1] - a[i]) * (a[i + 1] - a[i]) > 0
(4 - 1) × (2 - 1) > 0
(3 - 6) × (4 - 6) > 0
或者
(a[i] - a[i - 1]) * (a[i + 1] - a[i]) < 0
#include <iostream>
using namespace std;
const int N = 1005;
int n, a[N], x;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int cnt = 0;
for (int i = 1; i + 1 < n; i++) {
if ((a[i - 1] - a[i]) * (a[i + 1] - a[i]) > 0) {
cnt++;
}
}
cout << cnt << endl;
return 0;
}
if ((a[i] - a[i - 1]) * (a[i + 1] - a[i]) < 0) {
也可以