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.
  • 相关阅读:
    DOS下读取4GB内存
    开始了
    PHP函数补完:var_export()
    php 调试相关
    css选择符
    javascript的urlencode
    用CSS代码绘制三角形 纯CSS绘制三角形的代码
    Jquery 操作Cookie
    提高PHP编程效率的53种方法
    执行phpinfo();时提示:date_default_timezone_set()
  • 原文地址:https://www.cnblogs.com/sdxk/p/4693723.html
Copyright © 2011-2022 走看看