F.中位数
Time Limit: 1000 MS | Memory Limit: 65536 KB |
Total Submissions: 37 | Accepted: 20 |
Description
计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小的顺序排列。如果数据的个数是奇数,则中间那个数据就是这群数据的中位数;如果数据的个数是偶数,则中间那2个数据的算术平均值就是这群数据的中位数。
现在给出n个正整数,求他们的中位数。
Input
第一行:n —— 数列数字的个数(1 <=n<=1000)。
第二行:有n个正整数,每两个数中间用空格隔开(每一个正整数都小于10000)。
Output
求这N个正整数的中位数(保留小数点后两位有效数字)。
Sample Input
4
1 2 3 4
3
1 2 3
1 2 3 4
3
1 2 3
Sample Output
2.50
2.00
2.00
Source
安徽省2014年“京胜杯”大学生程序设计竞赛
水题
/**
* Project Name: 省赛
* File Name: F中位数.cpp
* Created on: 2015年4月26日 下午2:16:00
* Author: jtahstu
* QQ: 1373758426 E-mail:1373758426@qq.com
* Copyright (c) 2015, jtahstu , All Rights Reserved.
*/
//Sample Input
//4
//1 2 3 4
//3
//1 2 3
//Sample Output
//2.50
//2.00
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main() {
int a[10005] = { 0 }, n;
while (cin >> n) {
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
double ans;
if (n % 2)
ans = a[n / 2];
else
ans = (a[n / 2] + a[n / 2 - 1]) * 1.0 / 2;
printf("%.2f\n", ans);
}
return 0;
}