| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
那位牛人,用qsort()搞定,进来帮我看下哈!搞不明白哪有错啊?#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
int cmp(const void *a, const void *b) {
if(*(unsigned int*)a>*(unsigned int*)b)
return 1;
else if(*(unsigned int*)a==*(unsigned int*)b) //还是看别人的后,加的,原因不明
return 0;
return -1;
}
void main() {
int size, i;
unsigned int *nm;
float ans;
scanf("%d", &size);
nm = new unsigned int[size];
for(i = 0; i < size; i++)
scanf("%d", &nm[i]);
qsort(nm, size, sizeof(nm), cmp);
if(size % 2 == 0)
ans = (nm[size/2-1] + nm[size/2]) / 2.0;
else
ans = nm[(size-1)/2];
printf("%.1f\n", ans);
delete[]nm;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator