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 ...
随机推荐
- ubuntu Server 14 自动更新
https://help.ubuntu.com/stable/serverguide/automatic-updates.html http://spin.atomicobject.com/2014/ ...
- 16.检查是否为BST
题目描述 请实现一个函数,检查一棵二叉树是否为二叉查找树. 给定树的根结点指针TreeNode* root,请返回一个bool,代表该树是否为二叉查找树 import java.util.*; /* ...
- VBA_Excel_教程:表,格
Sub 表和格() '定义工作表 Dim ws As Worksheet 'get sheet by name[看到的表名,或序号1,2,3,...],要加Set Set ws = Worksheet ...
- postman+newman(2)
用newman执行带环境变量的postman测试用例 1.在postman中将用例项目文件导出外还需将环境变量文件导出,如下: 2.newman执行如下: newman -c 测试用例文件 -e 环境 ...
- 模块化InnoSetup依赖项安装
原文在这里:http://www.codeproject.com/Articles/20868/NET-Framework-Installer-for-InnoSetup 源文件地址:http://w ...
- css之让文字在一定范围内显示,不超过固定的宽度和高度
- 属性传值,协议传值,block传值,单例传值四种界面传值方式
一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N ...
- tomcat7的安装与配置、及Servlet部署
一.tomcat7的下载页面 https://tomcat.apache.org/download-70.cgi 就是:32-bit Windows zip 下载得到apache-tomcat-7.0 ...
- loadrunner工具使用之脚本创建
loadrunner工具使用之脚本创建 一.创建脚本 1.打开loadrunner,选择第一个控件VuGen(创建/编辑脚本),点击
- Jsoncpp的使用
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...