题解 | #打印极值点下标#
打印极值点下标
https://www.nowcoder.com/practice/7fd72f8ac7964ba3b8baa8735246e1f1
#include<cstdio> #include<vector> using namespace std; int main(){ int n; while (scanf("%d", &n) != EOF){ vector<int> vec; vector<int>ans; for (int i = 0; i < n; i++){ int q; scanf("%d", &q); vec.push_back(q); } if (vec[0] != vec[1]){ ans.push_back(0); } for (int i = 1; i < vec.size()-1; i++){ if (vec[i]<vec[i - 1] && vec[i]<vec[i + 1] || vec[i]>vec[i - 1] && vec[i]>vec[i + 1]){ ans.push_back(i); } } if (vec[vec.size()-2] != vec[vec.size()-1]){ ans.push_back(vec.size()-1); } printf("%d", ans[0]); if (ans.size()>1){ for (int i = 1; i < ans.size(); i++){ printf(" %d", ans[i]); } } printf("\n"); } }