HDOJ 1157 Who's in the Middle

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

  • 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

Hint

INPUT DETAILS:

Five cows with milk outputs of 1..5

OUTPUT DETAILS:

1 and 2 are below 3; 4 and 5 are above 3.

一道求中位数的简单题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<iostream>

#include<algorithm>

using namespace std;
bool cmp(int a,int b){
    return a>b;
}//从大到小排序;

int main(){
    int n;
    while(scanf("%d",&n)==1){
    int i,j,arr[10005];
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
         sort(arr,arr+n,cmp);
    printf("%d\n",arr[(n-1)/2]);
    }
    return 0;
}

下面用快排做:

/**#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<iostream>

#include<algorithm>

using namespace std;
bool cmp(int a,int b){
    return a>b;
}//从大到小排序;

int main(){
    int n;
    while(scanf("%d",&n)==1){
    int i,j,arr[10005];
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
         sort(arr,arr+n,cmp);
    printf("%d\n",arr[(n-1)/2]);
    }
    return 0;
}
**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<iostream>

#include<algorithm>

using namespace std;
int cmp(const void *x,const void *y){
    return (*(int *)x-*(int *)y);//从小到大
   /** return (*(int *)y-*(int *)x);//从大到小**/
}

int main(){
    int n;
    while(scanf("%d",&n)==1){
    int i,j,arr[10005];
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
         qsort(arr,n,sizeof(int),cmp);
       /**  for(int i=0;i<n;i++){
            printf("%d\n",arr[i]);
         }**/
    printf("%d\n",arr[(n-1)/2]);
    }
    return 0;
}

时间: 2024-10-04 06:11:09

HDOJ 1157 Who&#39;s in the Middle的相关文章

HDOJ 1163 Eddy&amp;#39;s digital Roots(九余数定理的应用)

Problem Description The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits a

HDOJ 1164 Eddy&amp;#39;s research I(拆分成素数因子)

Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to h

HDOJ 1210 Eddy&amp;#39;s 洗牌问题

Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于纸牌也有一定的研究,他在无聊时研究发现,如果他有2N张牌,编号为1,2,3..n,n+1,..2n.这也是最初的牌的顺序.通过一次洗牌可以把牌的序列变为n+1,1,n+2,2,n+3,3,n+4,4..2n,n.那么可以证明,对于任意自然数N,都可以在经过M次洗牌后第一次重新得到初始的顺序.编程对于小于100000的自然数N,求出M的值. Input 每行一个整数N Output 输出与之对应的M Sa

HDOJ 1397 Goldbach&amp;#39;s Conjecture(快速筛选素数法)

Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been proved nor refused yet. No one is sure whether

HDOJ 2200 Eddy&amp;#39;s AC难题(数学组合概率题)

Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目的数量摘录下来,然后从中选择一部分人(或者全部)按照ac的数量分成两组进行比较,他想使第一组中的最小ac数大于第二组中的最大ac数,但是这样的情况会有很多,聪明的你知道这样的情况有多少种吗? 特别说明:为了问题的简化,我们这里假设摘录下的人数为n人,而且每个人ac的数量不会相等,最后结果在64位整数

HDOJ 1098 Ignatius&amp;#39;s puzzle

Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal nonegative integer

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 2140 Michael Scofield&amp;#39;s letter(字符转换~)

Problem Description I believe many people are the fans of prison break. How clever Michael is!! In order that the message won't be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the he

HDOJ/HDU 1250 Hat&amp;#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take