who is the middle

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

  

时间: 2025-01-01 21:00:05

who is the middle的相关文章

HDU1157 Who&#39;s in the Middle

Who's in the Middle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9411    Accepted Submission(s): 4538 Problem Description FJ is surveying his herd to find the most average cow. He wants to k

垂直居中小记 line-height table vertical-align:middle

垂直居中分两种情况:1.父元素高度确定的单行文本        2.以及父元素高度确定的多行文本. 1.垂直居中-父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的 height 和 line-height高度一致来实现的,即此时单行文本的行高line-height=height(父元素块高度).(height: 该元素的高度,line-height: 顾名思义,行高(行间距),指在文本中,行与行之间的 基线间的距离 ). line-height 与 font-size 的计算值之差,在

利用伪元素对块级元素应用vetical-align:middle使之垂直居中

vetical-align的功能是使行内元素垂直对齐. 可能的值 baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶端与父元素字体的顶端对齐 middle 把此元素放置在父元素的中部. bottom 把元素的顶端与行中最低的元素的顶端对齐. text-bottom 把元素的底端与父元素字体的底端对齐. length   % 使用 "line-height&qu

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

vertical-align:middle和overflow:hidden;

如果想让一个div或一张图片相对于整个页面居中,用vertical-align:middle可以很简单地解决. 在设置了 width 或 height 的div中,写上overflow:hidden;的话,超出宽度或高度的部分,就隐藏了.就是说,这个overflow:hidden;属性可以保证div的高度或宽度不变.div里添加的东西再多,高度或宽度也不变.超出的部分隐藏.

BZOJ 2653: middle

2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1536  Solved: 855[Submit][Status][Discuss] Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[c,d]之间的子序列中,最大的中位数. 其中a<b<c<d. 位置

POJ 2388 Who&#39;s in the Middle(水~奇数个数排序求中位数)

题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: 1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int main() 5 { 6 int n; 7 while(scanf("%d",&n)!=EOF) 8 { 9 int na[n+1]; 10 for(int i=0; i

G - Who&#39;s in the Middle

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,0

重构改善既有代码设计--重构手法15:Remove Middle Man (移除中间人)

某个类做了过多的简单委托动作.让客户直接调用受托类. 动机:在Hide Delegate (隐藏委托关系)的“动机”中,谈到了“封装委托对象”的好处.但是这层封装也是要付出代价的,它的代价是:每当客户要使用受托类的新特性时,你就必须在服务端添加一个简单委托函数.随着委托类的特性(功能)越来越多,这一过程让你痛苦不已.服务类完全变成了“中间人”,此时你就应该让客户直接调用受托类. 很难说什么程度的隐藏才是合适的.还好,有了Hide Delegate (隐藏委托关系)和Remove Middle M

DAYDREAM & MIDDLE WARE

Daydream VR潮流下,企业自己建立中间件的意义 by 高煥堂 在Daydream潮流下,移动VR将会流行,但是简单的Controller并未让广大开发者满意,由于三维交互攸关外设配备,且百花齐放.不断更新以求提升体验性,像传统有框的二维UI的标准化.跨平台性,对VR App开发而言,短期内仍是乌托邦. 因此,Daydream将激发更多企业增添自己的外围设备,来凸显自己产品的优质沉浸体验. 由于这些外围硬件设备,与手机的直接衔接点是驱动软件.驱动软件又是在Android底层的HAL/Lin