码迷,mamicode.com
首页 > 其他好文 > 详细

百度-2017-买帽子

时间:2017-08-17 22:50:29      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:价格   class   div   its   描述   ring   for   clu   iar   

题目描述:

度度想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同,度度想买一顶价格第三便宜的帽子,问第三便宜的帽子价格多少?

输入描述:

首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000)

输出描述:

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

输入例子:

10
10 10 10 10 20 20 30 30 40 40

输出例子:

30

set——> C++的SET 内部是有序的。

 1 //
 2 // Created by ProMoriarty on 2017/8/17.
 3 //
 4 #include <bits/stdc++.h>
 5 #include<iostream>
 6 #include<stdio.h>
 7 #include<math.h>
 8 #include<string.h>
 9 using namespace std;
10 set<int> s;
11 int main(){
12     int n;
13     scanf("%d",&n);
14     for(int i=0;i<n;i++){
15         int x;
16         scanf("%d",&x);
17         s.insert(x);
18     }
19     int cnt = 0;
20     set<int>::iterator it;
21     for(it = s.begin();it!=s.end();it++){
22         cnt++;
23         if(cnt==3){
24             printf("%d\n",*it);
25             break;
26         }
27     }
28     if(cnt<3)
29         printf("-1\n");
30     return 0;
31 }

 

百度-2017-买帽子

标签:价格   class   div   its   描述   ring   for   clu   iar   

原文地址:http://www.cnblogs.com/awpboxer/p/7384702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!