问题 B: 打印极值点下标
时间限制 : 1.000 sec 内存限制 : 32 MB
题目描述
在一个整数数组上,对于下标为i的整数,如果它大于所有它相邻的整数,或者小于所有它相邻的整数,则称为该整数为一个极值点,极值点的下标就是i。
输入
每个案例的输入如下:
有2×n+1行输入:第一行是要处理的数组的个数n;
对其余2×n行,第一行是此数组的元素个数k(4<k<80),第二行是k个整数,每两个整数之间用空格分隔。
输出
每个案例输出不多于n行:每行对应于相应数组的所有极值点下标值,下标值之间用空格分隔,如果没有极值点则不输出任何东西。
样例输入
2
4
1 2 1 3
5
3 4 5 6 7
样例输出
0 1 2 3
0 4
#include<iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
int a[85];
while (cin >> n) {
while (n--) {
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
cin >> a[i];
}
bool flag = false;
for (int i = 0; i < m; ++i) {
if (!i and (a[0] > a[1] or a[0] < a[1])) {
flag = true;
cout << 0 << " ";
} else if (i + 1 == m and (a[i] > a[i - 1] or a[i] < a[i - 1])) {
cout << m - 1;
flag = true;
} else if (a[i] > a[i - 1] and a[i] > a[i + 1] or a[i] < a[i - 1] and a[i] < a[i + 1]) {
flag = true;
cout << i << " ";
}
}
if (flag)
cout << endl;
}
}
return 0;
}