ZCMU 1171: 松哥的众数(dp)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 531  Solved: 180
 

Description

有n个数,其中有不少于n/2个数为x,你能告诉松哥x是多少嘛?

Input

多组测试数据。每组测试数据第一行有一个正整数n(n<=100000),

第二行为n个绝对值不超过100000的整数。

Output

对于每组测试数据输出x。

Sample Input

5 2 1 2 3 2 8 3 3 4 4 4 4 3 4

Sample Output

2 4

HINT

Source

题解:这道题按照记录每一个数字大小后再比数量做会有超时,今天在杭电oj做1029,发现题目类似。注意题目中讲的至少n/2个数为x,那么x肯定是中位数。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int a[n];
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        printf("%d\n",a[n/2]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42671353/article/details/81353804
今日推荐