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
1 2 3 4
3
1 2 3
Sample Output
2.00
Source
/**
* Project Name: 省賽
* File Name: F中位數.cpp
* Created on: 2015年4月26日 下午2:16:00
* Author: jtahstu
* QQ: 1373758426 E-mail:[email protected]
* 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;
}