zoukankan      html  css  js  c++  java
  • Who's in the Middle

    题意;
    寻找中位数
    利用快速排序来寻找中位数。
     1 #include <iostream>
     2 using namespace std;
     3 int N;
     4 int op[10000];
     5 int Median(int left,int right,int pos){
     6     int l=left-1,r=left;
     7     int pirior=op[right];
     8     for(int i=left;i<right;i++){
     9         if(op[i]<pirior){
    10             int temp=op[i];
    11             op[i]=op[r];
    12             op[r]=temp;
    13             r++;
    14             l++;
    15         }
    16     }
    17     int t=op[right];
    18     op[right]=op[r];
    19     op[r]=t;
    20     if(r==pos){
    21         return op[r];
    22     }else if(r<pos){
    23         return Median(r+1,right,pos);
    24     }if(r>pos){
    25         return Median(left,r-1,pos);
    26     }
    27 
    28 }
    29 int main() {
    30     cin>>N;
    31     for(int i=0;i<N;i++){
    32         cin>>op[i];
    33     }
    34     int mid=Median(0,N-1,N/2);
    35     cout<<mid<<endl;
    36     return 0;
    37 }
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 34962   Accepted: 20387

    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.
  • 相关阅读:
    Redhat各个版本和内核对照
    Java8 lambda表达式总结
    conda 安装指定版本的指定包
    git初始化的几句shell
    MYsqli 绑定插入与查询实例
    按天去除重复数据,为0则取0,否则取最大的那个值
    存储过程,循环插入1000条记录
    主表如何统计在附表中的出现次数?
    Invalid argument supplied for foreach()
    二十、mysql mysqldump备份工具
  • 原文地址:https://www.cnblogs.com/sdxk/p/4693723.html
Copyright © 2011-2022 走看看