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

WHY? 又是一次挂在 QSORT 上了 ? 这样为什么会错呢?

Posted by acmpkacm at 2007-01-28 11:30:54 on Problem 2291
Problem Id:2291  User Id:acmpkacm 
Memory:28K  Time:0MS
Language:C++  Result:Wrong Answer

Source 

#include "stdio.h"
#include "stdlib.h"
long rope[10001];

int cmp(const void *a,const void *b)
{
	long *x=(long *)a;
	long *y=(long *)b;
	if(*x>*y) return 1; //这儿改成return(*x-*y) ; 就对了了呢?? 难道这么用错了吗?
	return 0;
}

int main()
{
	int t,m,i,j;
	long max;
	scanf("%d",&t);
	while(t--)
	{
		max=0;
		scanf("%d",&m);
		for(i=0;i<m;i++)
			scanf("%d",&rope[i]);
        qsort(rope,m,sizeof(long),cmp);
	    for(j=m,i=0;j>0;i++,j--)
		{
			if(max < rope[i] * j )
				max = rope[i] * j;
		}
        printf("%ld\n",max);
	}
	return 0;
}


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