- /* CCF201612-1 中间数 */
- #include <iostream>
- #include <map>
- using namespace std;
- int main()
- {
- map<int, int> m; //m为一个有两列的表,第一列维数组下标,第二列为对应的值
- int n, v;
- // 输入数据,构建Map
- cin >> n;
- for(int i=0; i<n; i++) {
- cin >> v;
- m[v]++;
- }
- // 找出中间数
- int ans, mid=(n+1)/2, count=0, left;
- for(map<int,int>::iterator it=m.begin(); it!=m.end(); it++) //迭代器 套路
- if(count + it->second >= mid) {
- left = count;
- count = 0;
- ans = it->first;
- } else
- count += it->second;
- // 输出结果
- if(left == count)
- cout << ans << endl;
- else
- cout << -1 << endl;
- return 0;
- }
map
最新推荐文章于 2024-04-16 07:29:58 发布