P16751 [GKS 2020 #B] Bike Tour 题解
题目大意
问在第 \(2\) 个到第 \(n-1\) 点中有多少个点高度比前后两个点的高度大。
算法分析
按题意模拟即可,枚举第 \(2\) 个到第 \(n-1\) 点,看是不是符合要求,最后把符合要求的点的数量加起来输出。
#include<bits/stdc++.h>
using namespace std;//参考代码
const int N=105;
int h[N];
int main(){
std::ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int T,id=0;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>h[i];
}
int ans=0;
for(int i=2;i<n;i++){
if(h[i]>h[i-1]&&h[i]>h[i+1]){
ans++;//如果符合要求
}
}
cout<<"Case #"<<++id<<": ";
cout<<ans<<'\n';
}
return 0;
}

浙公网安备 33010602011771号