您的位置:首页 > 其它

贪心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);
}

感想:这个题应该挺简单的,我却交了好多,先是用的冒泡排序没有过
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: