who is the middle
Description
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
* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
Output
Sample Input
2
4
1
3
5
Sample Output
解题思路:
简单的排序问题,平时排序问题虽说简单可是比较繁琐(冒泡法、选择法、折半查找) ,STL中自带了排序函数sort用起来非常方便让你从排序中解放出来。
给出两种不同方法读者自行比较:
代码一常规方法:
#include "cstdio"
#include "iostream"
#include "cstring"
#include "algorithm"
using namespace std; int main()
{
int a[10000];
int n,m;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
m=a[i];
a[i]=a[j];
a[j]=m; }
printf("%d\n",a[n/2]);}
return 0;
}
代码二(sort):
#include "cstdio"
#include "iostream"
#include "cstring"
#include "algorithm"
using namespace std; int main()
{
int a[10000];
int n,m;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
printf("%d\n",a[n/2]);}
return 0;
}
who is the middle的更多相关文章
- 代码的坏味道(21)——中间人(Middle Man)
坏味道--中间人(Middle Man) 特征 如果一个类的作用仅仅是指向另一个类的委托,为什么要存在呢? 问题原因 对象的基本特征之一就是封装:对外部世界隐藏其内部细节.封装往往伴随委托.但是人们可 ...
- 【BZOJ-2653】middle 可持久化线段树 + 二分
2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1298 Solved: 734[Submit][Status][Discu ...
- Who's in the Middle[HDU1157]
Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...
- vertical-align:middle
img style="vertical-align:middle;" src="<%=basePath%>/images/ckpy.png" > ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 运用vertical:middle 使得元素垂直居中
我要垂直居中我要垂直居中我要垂直居中我要垂直居中我要垂直居中垂直居中垂直居中垂直居中中垂直居中垂直居中 <!-- 第一步:主题元素display:inline-block;第二步:插入辅助元 ...
- Who's in the Middle 分类: POJ 2015-06-12 19:45 11人阅读 评论(0) 收藏
Who's in the Middle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34155 Accepted: 1 ...
随机推荐
- view--4种Android获取View宽高的方式
有时我们会有基于这样的需求,当Activity创建时,需要获取某个View的宽高,然后进行相应的操作,但是我们在onCreate,onStart中获取View的大小,获取到的值都是0,只是由于View ...
- 整理QQ数据库sql语句
设置数据库的时候 qq 号如果用整型,设置成UNSIGNED,不然超过一定数值就错误 UPDATE `sao_qq` SET qq_num = REPLACE ( qq_num, '@qq.com', ...
- css学习笔记(5)
<style>*{margin:0; padding:0;}ul{ list-style:none;}li{ height:30px; width:100px; background:#F ...
- centos之Haproxy 负载均衡学习笔记
HAProxy的特点是:1.支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机:2.能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作3.支持url检测后端的 ...
- 多个字段用and和or时要注意用括号。
多个字段用and和or时要注意用括号. 新技能get! create table wly_test (name1 varchar2(10),number1 number(6),score1 numbe ...
- 1. IOS 9.3.3描述文件没了处理方法
1.用手机登录:https://beta.apple.com/ 2.找到"注册您的设备" 3.往下拉第二点就是,直接点击下载即可.
- C++ 类里面,函数占用存储空间问题
转载自:http://blog.163.com/xping_lsr/blog/static/19654034520119804131721/ 先看两段代码: 代码段1:class A{public:i ...
- redis基础使用
redis分linux,window两个版本分支. redis在window下的使用先下载相关包.下载地址:https://github.com/MSOpenTech/redis/releases 下 ...
- JavaWeb项目连接Oracle数据库
参考网址:http://jingyan.baidu.com/article/0320e2c1d4dd0b1b87507b38.html 既然你要链接oracle数据库 ,那么首先就是先打开我们的ora ...
- rtsp 协议 详细讲解
转载自:http://www.mikewootc.com/wiki/net/protocol/rtsp.html 目录: 概述 RTSP简介 协议特点 协议细节 典型的rtsp交互过程 RTSP消息格 ...