有N个数(N为奇数且1<=N<=10,000),现在需要求出这些数的中位数
Input
-
Line 1: A single integer N
-
Lines 2…N+1: Each line contains a single integer that is the milk output of one cow.
Output
- Line 1: A single integer that is the median milk output.
Sample Input
5
2
4
1
3
5
Sample Output
3
Hint
INPUT DETAILS:
Five cows with milk outputs of 1…5
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.
#include<stdio.h>
#include<stdlib.h>
void sort(int *a,int count)
{
int temp;
int i;
int j;
for(i=0;i<count-1;i++)
{
for(j=i+1;j<count;j++)
{
if(*(a+i)>*(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
}
}
int main()
{
int *a;
int count,mid;
int i;
scanf("%d",&count);
a=(int *)malloc(sizeof(int)*count);
if(count%2!=0)
mid=(count+1)/2-1;
else
mid=count/2-1;
for(i=0;i<count;i++)
{
scanf("%d",a+i);
}
sort(a,count);
printf("%d\n",*(a+mid));
free(a);
a=NULL;
return 0;
}