洛谷P1317 低洼地

题目链接

题目描述
一组数,分别表示地平线的高度变化。高度值为整数,相邻高度用直线连接。找出并统计有多少个可能积水的低洼地?

如图:地高变化为 0 1 0 2 1 2 0 0 2 0

输入格式
两行,第一行n,表示有n个数。第2行连续n个数表示地平线高度变化的数据,保证首尾为0。(3<=n<=10000,0<=高度<=1000)

输出格式
一个数,可能积水低洼地的数目。

输入输出样例
输入 #1
10
0 1 0 2 1 2 0 0 2 0
输出 #1
3

代码:

//P1317 低洼地
#include<iostream>
using namespace std;
int num[100005] = {
    
    0};
int main()
{
    
    
	int n, m, count = 0, flag = 0;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> num[i];
	for(int i = 1; i < n; i++)
		if(num[i] > num[i - 1])
		{
    
    
			if(flag == -1) count++;
			flag = 1;
		}
		else if(num[i] < num[i - 1]) flag = -1;
	cout << count << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44826711/article/details/113769865