现在的位置: 首页 > 综合 > 正文

hdu 1157 Who’s in the Middle (水题,中位数)

2017年10月18日 ⁄ 综合 ⁄ 共 457字 ⁄ 字号 评论关闭

小记:GNU C++硬是要我把变量都定义到main()函数外才能让我AC...

思路:读入数据存入数组,排序,输出中间那个,输入保证是奇数个。从0开始存,答案就是排好序后的第n/2个

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8

const int MAX_ = 10010;

int p[MAX_], n;

int main(){
	while(~scanf("%d",&n)){
        for(int i = 0; i < n; ++i){
            scanf("%d",&p[i]);
        }
        sort(p,p+n);
        printf("%d\n",p[n/2]);
	}
	return 0;
}

抱歉!评论已关闭.