我现在做的是编号为1016的试题,具体内容如下所示:
Problem Q
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 239 Accepted Submission(s) : 111
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.
5 2 4 1 3 5
3
输入n个奶牛的牛奶量,找到其中的一个牛奶量,至少位于这些牛奶量的中间位置
解题思路:
先对数据进行降序排列,然后找到(n-1)/2;
编写代码:
#include <iostream>
#include <stdlib.h>
using namespace std;
int a[100];
int cmp(const void * x,const void* y)
{
return (*(int*)x - *(int*)y);
}
int main(void)
{
int n,i;
while(cin >> n)
{
for(i = 0; i<n; i++)
{
cin >> a[i];
}
qsort(a,n,sizeof(int),cmp);
cout << a[(n-1)/2] << endl;
}
return 0;
}