美文网首页将来跳槽用
2017春招百度编程题

2017春招百度编程题

作者: 落夏简叶 | 来源:发表于2017-08-07 18:21 被阅读13次

1 [编程题] 买帽子

时间限制:1秒
空间限制:32768K
度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同。度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少?
输入描述:
首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000)

输出描述:
如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1

输入例子1:
10
10 10 10 10 20 20 30 30 40 40

输出例子1:
30

提交成功代码

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 60;
int main(int argc, const char * argv[]) {
    int n;
    int a[maxn];
    while (cin>>n) {
        for (int i = 0; i < n; i++) {
            cin>>a[i];
        }
        sort(a,a+n);
        vector<int> vector1;
        for (int j = 0; j < n; j++) {
            if (a[j] != 0) {
                vector1.push_back(a[j]);
            }
        }
        
        vector1.erase(unique(vector1.begin(), vector1.end()),vector1.end());
        if (vector1.size() > 2) {
            cout<<vector1[2]<<endl;
        }else {
            cout<<"-1"<<endl;
        }   
    }    
    return 0;
}

相关文章

网友评论

    本文标题:2017春招百度编程题

    本文链接:https://www.haomeiwen.com/subject/glxwfxtx.html