OpenJ_Bailian - 2691打印极值点下标 C++实现

打印极值点下标

问题描述
在一个整数数组上,对于下标为i的整数,如果它大于所有它相邻的整数,或者小于所有它相邻的整数,则称为该整数为一个极值点,极值点的下标就是i。

Input
有2×n+1行输入:第一行是要处理的数组的个数n;对其余2×n行,第一行是此数组的元素个数k(4<k<80),第二行是k个整数,每两个整数之间用空格分隔。

Output
输出为n行:每行对应于相应数组的所有极值点下标值,下标值之间用空格分隔。

Sample Input

3
10
10 12 12 11 11 12 23 24 12 12
15
12 12 122 112 222 211 222 221 76 36 31 234 256 76 76 
15
12 14 122 112 222 222 222 221 76 36 31 234 256 76 73 

Sample Output

0 7
2 3 4 5 6 10 12
0 2 3 10 12 14

实现代码

 #include<iostream>
 #include<list>
 using namespace std;
    
 int main() {
    int n;
    list<int>::iterator it;
    cin >> n;
    while (n--)
    {
    	int m;
    	list<int> array;
    	int *a;
    	cin >> m;
    	a = (int *)malloc(sizeof(int)*m);
    	for (int i = 0; i < m; i++) {
    		cin >> a[i];
    	}
    
    	for (int i = 0; i < m; i++) {
    		if (i == 0 && a[i] != a[i + 1]) array.push_back(i);
    		else if(i == m-1 && a[i] != a[i - 1]) array.push_back(i);
    		else {
    			if(a[i]>a[i-1]&&a[i]>a[i+1]) array.push_back(i);
    			else if (a[i] < a[i - 1] && a[i] < a[i + 1]) array.push_back(i);
    		}
    	}
    	int flag = 0;
    	for (it = array.begin(); it != array.end(); ++it) {
    		if (flag == 0) cout << *it;
    		else cout << " " << *it;
    		flag++;
    	}
    	cout << endl;
    }
    return 0;
 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值