首页 > 代码库 > hdu_1029_Ignatius and the Princess IV(模拟)

hdu_1029_Ignatius and the Princess IV(模拟)

http://acm.hdu.edu.cn/showproblem.php?pid=1029

#include <iostream>
#include <cstring>
using namespace  std;

void f(int *a,int len)
{
    int i = 0;
    int cnt = 1;
    while(i < len)
    {
        int temp = a[i];
        while(a[i] == a[i+1])
        {
            cnt++;
            i++;
        }
        i++;
        if (cnt >= (len+1)/2)
        {
            cout << temp;
            break;
        }
        cnt = 1;
    }
}

int cmp( const void *a , const void *b )
{
    return *(int *)a - *(int *)b;
}

int main()
{
    int n;
    while(cin >> n)
    {
        int *a = new int[999999];
        for (int i = 0;i < n;i++)
        {
            cin >> a[i];
        }
        qsort(a,n,sizeof(a[0]),cmp);
        f(a,n);
        cout << endl;
        delete[] a;
    }
                
    return 0;
}


hdu_1029_Ignatius and the Princess IV(模拟)