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

打印极值点下标

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

纯模拟

#include <iostream>
using namespace std;

const int N = 150;

int a[N];
int n;

int main()
{
    while (cin >> n)
    {
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        //特判左
        if (a[1] > a[0] || a[1] < a[0])
            cout << 0 << " ";
        
        for (int i = 1; i < n - 1; ++i)
        {
            int l = i - 1, r = i + 1;
            if (a[l] > a[i] && a[r] > a[i])
                cout << i << " ";
            else if (a[l] < a[i] && a[r] < a[i])
                cout << i << " ";
        }
        //特判右
        if(a[n-2] > a[n-1] || a[n-2] < a[n-1])
            cout << n - 1 << " ";
        cout << endl;
    }
    return 0;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务