HDU1029 Ignatius and the Princess IV

February 17, 2016

题目链接

题意:给 n(奇数)个数,找到一个数,这个数最少出现(n+1)/ 2 次。

sort 一下输出中间位置的数就可以了。。看讨论版有 map 搞的,有 DP 的。。。根本不需要。

#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int a[1000000];
int main(){
    //freopen("a.txt", "r", stdin);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; i++){
            scanf("%d", &a[i]);
        }
        sort(a, a+n);
        int ans = (n+1)/2;
        cout << a[ans] << endl;
    }
    return 0;
}

Profile picture

Written by Armin Li , a venture capitalist. [Weibo] [Subscribe]