贪心1016
2016-03-22 22:02
309 查看
原题:
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
* Line 1: A single integer N <br> <br>* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
[align=left]Output[/align]
* Line 1: A single integer that is the median milk output.
[align=left]Sample Input[/align]
5
2
4
1
3
5
[align=left]Sample Output[/align]
3
代码:#include<stdio.h>
main()
{
int
n,i,j,a[10001],temp=0,flag;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-1-i;j++)
<
d15b
span style="color:rgb(73,73,73);font-family:simsun;font-size:14px;line-height:21px;background-color:rgb(226,221,199);"> {
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if(flag==0)break;
}
printf("%d",a[n/2]);
//scanf("%d",&n);
}
感想:这个题应该挺简单的,我却交了好多,先是用的冒泡排序没有过
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
* Line 1: A single integer N <br> <br>* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
[align=left]Output[/align]
* Line 1: A single integer that is the median milk output.
[align=left]Sample Input[/align]
5
2
4
1
3
5
[align=left]Sample Output[/align]
3
代码:#include<stdio.h>
main()
{
int
n,i,j,a[10001],temp=0,flag;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-1-i;j++)
<
d15b
span style="color:rgb(73,73,73);font-family:simsun;font-size:14px;line-height:21px;background-color:rgb(226,221,199);"> {
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if(flag==0)break;
}
printf("%d",a[n/2]);
//scanf("%d",&n);
}
感想:这个题应该挺简单的,我却交了好多,先是用的冒泡排序没有过
相关文章推荐
- K-means聚类算法
- iOS 用到的宏
- JDK安装与环境变量的配置
- Android实现动态显示或隐藏密码输入框的内容
- poj 2778:DNA Sequence
- CentOS 6.6安装Xtrabackup RPM提示缺少libev.so.4()
- error LNK 2019
- Handler机制
- USB的内核线程 与 完成量之间的关系 赏析
- 常见文件操作代码(文件管理软件)
- 面试题58:检查单链表是否回文
- 给单文档框架添加背景图
- poj2249
- butterknife简介及Generate ButterKnife Injections 不出现的问题解决
- int8_t int16_t int32_t int64_t
- rucheng_hjp,如何获取iframe内容
- opengles绘图(3)
- 打开新链接方式总结
- Android开发学习之路-回调机制学习笔记
- Linux 安装php (tar方式)