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. ASP.NET中进行消息处理(MSMQ) 三(转)

    在本文的前两篇文章里对MSMQ的相关知识点进行了介绍,很多阅读过这前两篇文章的朋友都曾问到过这样一些问题:  1.如何把MSMQ应用到实际的项目中去呢?  2.可不可以介绍一个实际的应用实例?  3. ...

  2. PHP连接SQLServer

    连接前配置系统: 1.检查文件 php5.2.5/ntwdblib.dll 默认下面有一个,不能连接再替换. 下载正确版本的 ntwdblib.dll (2000.80.194.0),地址: http ...

  3. tomcat + apache +jkmod 配置php,jsp共存

    httpd.conf ##############################################################################配置phpLoadMo ...

  4. memcache c++使用

    memcache作为一个内存数据库,快速查询的功能. 在c++下面使用memcache需要一个memcache client.c++版本的是libmemcached http://libmemcach ...

  5. 深入理解JS异步编程二(分布式事件)

    PubSub模式 从原生的js角度,我们要监听某事件的方法就是利用addEventListener方法,但是当我们的页面趋于复杂,比如要向某个元素添加多个处理事件,那么就要用一个封装函数汇集多个处理函 ...

  6. 转:RTC搭建android下三层应用程序访问服务器MsSql-服务器端

    原文:http://www.cnblogs.com/delphi007/p/3346061.html 前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库 ...

  7. linux部分命令

    打包ROOT文件夹tar -cvf ROOT3.tar ./ROOT 解压则相反 ps aux | grep tomcat 直接杀死tomcat sudo kill -9 进程号 rm -rf ./R ...

  8. ios block和函数的区别

    block是封装了一段代码的OC对象,可以被设为Property, 在调用block的地方block都会被替换成相应的代码,相当于内联函数. 函数可以使代码更加整洁易读,使用block会使代码可读性变 ...

  9. 【转载】OpenGL ES 三种类型修饰 uniform attribute varying

    其实attribute varying已经被in和out代替了,但是有些工程代码里面仍然还在,所以权当笔记好了. 1.uniform变量uniform变量是外部application程序传递给(ver ...

  10. gulp-htmlmin压缩html

    通过一条命令用Npm安装gulp-htmlmin: npm install gulp-htmlmin --save-dev 安装完毕后,打开gulpfile.js文件,我们里面编写一个task用来专门 ...