Week12-必做題1(簽到)

問題描述

給出n個數,zjm想找出出現至少(n+1)/2次的數, 現在需要你幫忙找出這個數是多少?

Input

本題包含多組數據:
每組數據包含兩行。
第一行一個數字N(1<=N<=999999) ,保證N爲奇數。
第二行爲N個用空格隔開的整數。
數據以EOF結束。

Output

對於每一組數據,你需要輸出你找到的唯一的數。

Sample input

5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1

Sample output

3
5
1

解題思路

map的基本應用

完整代碼

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;

int n;
map<int,int> mp;
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    while(cin>>n){
        int ans=0;
        for (int i=1; i<=n; i++){
            int temp; cin>>temp;
            mp[temp]++;
            if(mp[temp]>=(n+1)/2){
                ans=temp;
            }
        }
        cout<<ans<<endl;
        mp.clear();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章