zoukankan      html  css  js  c++  java
  • NUC1371 Who's in the Middle【中位数+排序】

    Who's in the Middle

    时间限制: 1000ms 内存限制: 65535KB

    通过次数: 1总提交次数: 1

    问题描述
    FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less.

    Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.
    输入描述
    * Line 1: A single integer N
    * Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
    输出描述
    * Line 1: A single integer that is the median milk output.
    样例输入
    5
    2
    4
    1
    3
    5
    
    样例输出
    3
    
    来源
    USACO 2004 November Gold
    提示
    INPUT DETAILS:
    Five cows with milk outputs of 1..5

    OUTPUT DETAILS:
    1 and 2 are below 3; 4 and 5 are above 3.


    问题分析:(略)

    这个问题和《POJ2388 HDU1157 Who's in the Middle【中位数+排序】》是同一个问题,代码直接用就AC了。

    程序说明:参见参考链接。

    参考链接:POJ2388 HDU1157 Who's in the Middle【中位数+排序】

    题记:程序做多了,不定哪天遇见似曾相识的。

    AC的C++程序如下:

    /* POJ2388 Who's in the Middle */
    
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int N = 10000;
    int a[N];
    
    int main()
    {
        int n;
    
        // 输入数据
        while(cin >> n) {
            for(int i=0; i<n; i++)
                cin >> a[i];
    
            // 排序
            sort(a, a+n);
    
            // 输出结果
            cout << a[n / 2] << endl;
        }
    
        return 0;
    }



  • 相关阅读:
    Spring 注解详解01
    java 排序算法
    Java 文件拼接器
    oracle join
    Oracle 去重
    Java 笔试面试
    Oracle 笔试题02
    jvm性能查看常用命令
    关于对JMM(java内存模型)的个人理解
    RSA前端加密后端解密避免出现明文密码
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563658.html
Copyright © 2011-2022 走看看