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

* 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
5
 

Sample Output

3

解题思路:

简单的排序问题,平时排序问题虽说简单可是比较繁琐(冒泡法、选择法、折半查找) ,STL中自带了排序函数sort用起来非常方便让你从排序中解放出来。

给出两种不同方法读者自行比较:

代码一常规方法:

#include "cstdio"
#include "iostream"
#include "cstring"
#include "algorithm"
using namespace std; int main()
{
int a[10000];
int n,m;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
m=a[i];
a[i]=a[j];
a[j]=m; }
printf("%d\n",a[n/2]);}
return 0;
}

代码二(sort):

#include "cstdio"
#include "iostream"
#include "cstring"
#include "algorithm"
using namespace std; int main()
{
int a[10000];
int n,m;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
printf("%d\n",a[n/2]);}
return 0;
}

  

who is the middle的更多相关文章

  1. 代码的坏味道(21)——中间人(Middle Man)

    坏味道--中间人(Middle Man) 特征 如果一个类的作用仅仅是指向另一个类的委托,为什么要存在呢? 问题原因 对象的基本特征之一就是封装:对外部世界隐藏其内部细节.封装往往伴随委托.但是人们可 ...

  2. 【BZOJ-2653】middle 可持久化线段树 + 二分

    2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1298  Solved: 734[Submit][Status][Discu ...

  3. Who's in the Middle[HDU1157]

    Who's in the Middle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  5. vertical-align:middle

    img style="vertical-align:middle;" src="<%=basePath%>/images/ckpy.png" > ...

  6. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  7. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  8. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. 运用vertical:middle 使得元素垂直居中

    我要垂直居中我要垂直居中我要垂直居中我要垂直居中我要垂直居中垂直居中垂直居中垂直居中中垂直居中垂直居中   <!-- 第一步:主题元素display:inline-block;第二步:插入辅助元 ...

  10. Who's in the Middle 分类: POJ 2015-06-12 19:45 11人阅读 评论(0) 收藏

    Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34155   Accepted: 1 ...

随机推荐

  1. centos 7 下安装Nginx

    下载Nginx wget nginx.tar.gz http://nginx.org/download/nginx-1.11.3.tar.gz 解压源码 .tar.gz 然后进入目录编译安装 cd n ...

  2. win10激活

    听了同事忽悠,说x230这款还在保,可以直接装win10,自动激活,结果悲剧.送到联想工作站需要摧毁所有数据,然后还要2.3个小时,遂在网上找了个方法,先用着,只是不知道什么时候又变成黑户: 以管理员 ...

  3. 在linux下获取帮助

    1.使用man手册页 man是一种显示Unix/Linux在线手册的命令.可以用来查看命令.函数或文件的帮助手册,另外它还可以显示一些gzip压缩格式的文件. 读者在遇到不懂的命令时,可以用man查看 ...

  4. windows下scrapy 的安装

    2016-07-18  20:27:53 安装python 根据你的需求下载python安装包,安装python(本文基于python27)https://www.python.org/downloa ...

  5. NandFlash读写

    1.NandFlash分类 根据物理结构上的区别,NandFlash主要分为如下两类:•SLC (Single Level Cell): 单层式存储•MLC (Multi Level Cell): 多 ...

  6. js实现基础运动

    首先我定义3个div每个div当我鼠标放上去的时候,他的宽度就表达,如下图 首先是样式和html代码 <style> div{ width:100px; height:50px; back ...

  7. Python函数中的参数(一)

    函数传递参数时的简要关键点: 1.参数的传递是通过自动将对象赋值给本地变量名来实现的.函数参数在实际中只是Python赋值的一个实例.因为引用是以指针的形式实现的,所有的参数实际上都是通过指针进行传递 ...

  8. 1.openssl genrsa

    genrsa用于生成RSA私钥.不会生成公钥,因为公钥提取自私钥,如果需要查看公钥或生成公钥,可以使用openssl rsa命令,后文介绍. man genrsa查询其用法. [root@xuexi ...

  9. Linux C学习笔记07--管道通信

    管道通信属于进程间通信的一种方式,使用方便,但是局限是父进程与子进程间的通信,下面是调试好的代码. 程序父进程创建2个管道,分别用于父进程写数据--子进程读数据和子进程写数据--父进程读数据: #in ...

  10. 继续努力刷题--BE STRONGER AND STRONGER

    Come on,baby~ (1)Contains Duplicate 有自己的思路:两个for双重循环直接一个个比较,但肯定不是最优解.所以,使用Set中的HashSet(一种没有重复元素的无序集合 ...