CCF breakpoint counting (full score code + problem-solving ideas: find rules and violent simulation) 201604-1

problem solving ideas

A turning point is a higher point or a lower point.
Count the number of turning points.
When a[i] > a[i - 1] && a[i] > a[i + 1], the point is a higher point, and the answer is + 1
When a[i] < a[i - 1] && a[i] < a[i + 1], the point is lower and answer + 1


Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <sstream>
#include <set>

using namespace std;

const int N = 1010;

int a[N];

int main()
{
    
    
    int n;
    cin >> n;

    for (int i = 1; i <= n; i ++) cin >> a[i];
    int res = 0;
    for (int i = 2; i < n; i ++)
    {
    
    
        if (a[i] > a[i - 1] && a[i] > a[i + 1]) res ++; //较高点
        if (a[i] < a[i - 1] && a[i] < a[i + 1]) res ++; //较低点
    }
    cout << res;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_51800570/article/details/129150901