HDU1029 找中间数

来源:互联网 发布:pc使用mac虚拟机下载 编辑:程序博客网 时间:2024/05/01 06:23

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

输入一串数字,确保里面有一个数字出现次数超过一半,输出这个数字。

如,输入5

1 3 2 3 3

输出3.

思路:若某个数出现次数一半,中间数肯定是该数。

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

原创粉丝点击