Who‘s in the Middle

有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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值