当前位置: 首页 > news >正文

poj 2388 Who'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,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.
二、题解
        这道题就是求输入数据排序后的中位数,所以最重要的就是排序了。排序方法有很多种,我这里用了快速排序。它的基本思想是:通过一趟 排序将要排序的 数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以 递归进行,以此达到整个数据变成有序序列。
三、java代码
import java.util.Scanner; 

public class Main {
	public static void QuickSort(int[] a){
		QSort(a,0,a.length-1);
	}
	
	public static void QSort(int[] a,int p,int r){
		if(p<r)
		{
			int q=Partition(a,p,r);
			QSort(a,p,q-1);
			QSort(a,q+1,r);
		}
	}
	
	public static int Partition(int[] a,int p,int r){
		int x=a[r];
		int i=p-1;
		for(int j=p;j<r;j++)
		{
			if(a[j]<=x){
				i=i+1;
				swap(a, i, j);
			}
		}
		swap(a, i+1, r);
		return i+1;
	}
	public static void swap(int[] a, int i,int j){
    	int temp;
    	temp=a[j];
    	a[j]=a[i];
    	a[i]=temp;
    }
    public static void main(String[] args) { 
       Scanner cin = new Scanner(System.in);
       int n=cin.nextInt();
       int[] a=new int[n];
       for(int i=0;i<n;i++){
    	   a[i]=cin.nextInt();
       }
       QuickSort(a);
       System.out.println(a[n/2]);
    } 
  } 


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/AndyDai/p/4734178.html

相关文章:

  • com.javax.servlet 慢慢看完慢慢学完
  • margin标记可以带一个、二个、三个、四个参数,各有不同的含义。
  • jQuery – 8.事件和事件参数
  • 面试题20:栈的压入、弹出序列
  • 求字符串组合
  • PHP event 事件机制
  • 基于协同过滤的推荐引擎
  • 连续加班易“脑残”,程序员做做白日梦未尝不是一件好事!
  • Manacher模板,kmp,扩展kmp,最小表示法模板
  • linux修改文件读写执行权限命令chmod
  • right-click an action, missing Go to slot
  • 零售门店促销创新的八个思路
  • 华为C8812获取对system分区的读写权限
  • C#路径的相关操作
  • 第八章 对象和数组
  • [译]如何构建服务器端web组件,为何要构建?
  • Angular Elements 及其运作原理
  • Apache Zeppelin在Apache Trafodion上的可视化
  • CEF与代理
  • HTTP 简介
  • input的行数自动增减
  • JavaScript类型识别
  • mongo索引构建
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Spark RDD学习: aggregate函数
  • spring boot 整合mybatis 无法输出sql的问题
  • 将回调地狱按在地上摩擦的Promise
  • 漂亮刷新控件-iOS
  • 前端技术周刊 2019-01-14:客户端存储
  • 如何在GitHub上创建个人博客
  • 数据科学 第 3 章 11 字符串处理
  • 智能合约Solidity教程-事件和日志(一)
  • 终端用户监控:真实用户监控还是模拟监控?
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • (BFS)hdoj2377-Bus Pass
  • (Forward) Music Player: From UI Proposal to Code
  • (Oracle)SQL优化技巧(一):分页查询
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (转)Unity3DUnity3D在android下调试
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .chm格式文件如何阅读
  • .net 4.0发布后不能正常显示图片问题
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .net 中viewstate的原理和使用
  • @data注解_一枚 架构师 也不会用的Lombok注解,相见恨晚
  • @synthesize和@dynamic分别有什么作用?
  • [ Algorithm ] N次方算法 N Square 动态规划解决
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname
  • [.NET]桃源网络硬盘 v7.4
  • [BZOJ1060][ZJOI2007]时态同步 树形dp