题解 | #打印极值点下标#

打印极值点下标

https://www.nowcoder.com/practice/7fd72f8ac7964ba3b8baa8735246e1f1

一直数组上溢,本来想说是不是给分配的内存少,超过30个的数组要分开几次存储,结果!只要把数组定义语句放在while里面就行了,写了好久。。

#include <iostream>
using namespace std;

int main() {
    int n;
    
    
    while (cin >> n) { // 注意 while 处理多个 case
       int *a = new int[n];
       int i;
        for (i = 0; i < n; i++){
            cin >> a[i];
        }
     
        for(int i = 0; i < n; i++){
            if (i == 0 && a[i] != a[i+1]) cout<<i<<" ";
            else if (a[i] > a[i+1] && a[i] > a[i-1]) cout<<i<<" ";
            else if (a[i] < a[i+1] && a[i] < a[i-1]) cout<<i<<" ";
            else if (i == n-1 && a[i-1] != a[i]) cout<<i<<" ";
        }
        cout<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务