P1317 低洼地

https://www.luogu.com.cn/problem/P1317

在这里插入图片描述

  • 左坡形成后,使用布尔变量标记为一个状态,当左坡形成时再有右坡出现,则进行计数
#include<iostream>
using namespace std;
int n,c;
bool l;
int main()
{
    cin >> n;
    int* a = new int[n+5];
    for(int i = 1;i <= n;i++)
        cin >> a[i];
    for(int i = 2;i < n;i++)
        {
            if(a[i] < a[i-1])
            l = 1;
            if(a[i] > a[i-1]&& l==1) // 这里还需要左坡已经形成
            {
                c++;
                l = 0;
            }
        }

    cout << c;

}

大佬

大佬都不用数组

for(int z=1;z<=n;z++)
{
    cin >>b;
    if(b<a) {l=1;}
    if(b>a&&l==1) {ans++;l=0;}
    a=b;
}
发布了372 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104299910