2016SDAU课程练习一1016

来源:互联网 发布:数控车床编程 编辑:程序博客网 时间:2024/04/29 18:31
Problem Q 

Problem Description
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.
 


Input
* Line 1: A single integer N <br> <br>* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
 


Output
* Line 1: A single integer that is the median milk output.
 


Sample Input
5
2
4
1
3



Sample Output

3

题意:平均值啦


思路:排序取中间的


感想:应该是最简单的吧,但是每次博客写完粘代码就手残刷新了。。刷新。。。刷新。。。。这题有毒。。。


AC代码:

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
using namespace std;
int main()
{
    int n,i,a,b,s;
    vector<int>v;
    while(cin>>n)
    {
        for(i=0;i<n;i++)
        {
            cin>>a;
            v.push_back(a);
        }
        sort(v.begin(),v.end());
        if(n%2!=0)
        {
            b=(n+1)/2;
            s=v[b-1];
        }
        else
        {
            b=n/2;
            s=(v[b-1]+v[b])/2;
        }
        cout<<s<<endl;
    }
    return 0;
}

0 0
原创粉丝点击