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.
  • 相关阅读:
    Nuget常用命令(转)
    Core使用SAP Web Service
    jquery.dataTables动态列--转
    jqGrid随窗口大小变化自适应大小-转
    30个值得推荐的数据可视化工具--转
    Automapper问题记录
    MVC及MVC Core在filter中如何获取控制器名称和Action名称
    .Net Core使用 MiniProfiler 进行性能分析(转)
    Core中使用Hangfire
    ASP.NET 多环境下配置文件web.config的灵活配置---转
  • 原文地址:https://www.cnblogs.com/sdxk/p/4693723.html
Copyright © 2011-2022 走看看