Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

那位牛人,用qsort()搞定,进来帮我看下哈!搞不明白哪有错啊?

Posted by woshiniya at 2007-03-18 22:08:13 on Problem 2623
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator