2014省賽-F中位數

F.中位數

Time Limit: 1000 MS Memory Limit: 65536 KB
Total Submissions: 37 Accepted: 20

Description


計算有限個數的數據的中位數的方法是:把所有的同類數據按照大小的順序排列。如果數據的個數是奇數,則中間那個數據就是這羣數據的中位數;如果數據的個數是偶數,則中間那2數據算術平均值就是這羣數據的中位數。

現在給出n個正整數,求他們的中位數。



Input


第一行:—— 數列數字的個數(1 <=n<=1000)

第二行:有n個正整數,每兩個數中間用空格隔開(每一個正整數都小於10000)


Output


求這N個正整數的中位數(保留小數點後兩位有效數字)


Sample Input


4
1 2 3 4
3
1 2 3

Sample Output


2.50
2.00

Source


安徽省2014年“京勝杯”大學生程序設計競賽

水題

/**
 * 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;
}


發佈了189 篇原創文章 · 獲贊 30 · 訪問量 160萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章