您的位置:首页 > 其它

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

2014-04-08 19:23 507 查看
小记: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: