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 ...
随机推荐
- 数据库 MySql(二)
MySQL(二) 1.外键及连表 外键 一个特殊的索引,只能是指定内容 CREATE TABLE part1 ( nid INT NOT NULL auto_increment PRIMARY KEY ...
- CyclicBarrier类合唱演绎
package a.jery; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorServi ...
- 翻译: TypeScript 1.8 Beta 发布
原文地址:https://blogs.msdn.microsoft.com/typescript/2016/01/28/announcing-typescript-1-8-beta/ 今天,我们发布了 ...
- COSBench添加driver负载机
说明:Driver是COSBench测试工具中对负载机的一种标记,相当于loadrunner中的负载发生器. 在使用COSBench进行云存储性能测试时,面对强大的云服务,如果只有单个driver负载 ...
- iperf3实践
The basic commands are the same for iperf and iperf3: SAMPLE IPERF/IPERF3 COMMANDS Server: iperf/ipe ...
- 执行powershell脚本
打开powershell运行窗口: powershell.exe C:\Users\Administrator\Desktop\a.ps1 -a $a. -a $a : vbs脚本路径(如 C:\h ...
- java se the operation is not applicable to the current selection
当新建某的类时,需要自动构建 set get方法时. 我们一般 会直接让Myeclipse自动生动.偶尔 .他会犯2. the operation is not applicable to the c ...
- 简洁既是美—用while语句复制数组
简洁既是美,程序员应尽量尝试编写简洁的表达式,争取用简单的代码来实现更多的功能,当然,这也要看情况了(有时候也得考虑程序运行的时间嘛). 在阅读C++Prime Plus到while语句时有一个讲一个 ...
- 在IIS6上部署MVC站点,Nhiernate数据库底层
服务器环境要求: Windows 2003 server + IIS6.0 1.必须安装.net framework 4.0, MVC 2.最好能安装.net framework 的sp1, 我们服务 ...
- 关于DCOM的安全性
关于DCOM的安全性 DCOM的安全性设置在注册表中. 2. 通过DCOMCNF.exe可以配置