#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int cost,weight;
};
int cmp (node a, node b)
{
return a.weight<b.weight;
}
int main()
{
int n;
int kinds,i;
double ans,temp,money;
node data[1000];
scanf("%d",&n);
while(n--)
{
ans=0;
scanf("%lf%d",&money,&kinds);
for(i=0;i<kinds;i++)
scanf("%d%d",&data[i].cost,&data[i].weight);
sort(data,data+kinds,cmp);
for(i=0;i<kinds;i++)
{
if(money>=data[i].cost*data[i].weight)
{
ans+=(double)data[i].weight;
money-=(double)data[i].cost*data[i].weight;
}
else
{
temp=(double)money/data[i].cost;
ans+=temp;
money-=(double)temp*data[i].cost;
}
}
printf("%.2lf\n",ans);
}
return 0;
}
如果把排序改成qsort的话,就可以通过,不知道为什么
http://www.acmerblog.com/hdu-2187-512-3340.html
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=3&problemid=14
提供给你一个思路,sort需要一个返回类型为bool的函数,结果只有两种: a < b,a >= b,你这里虽然是int,其实调用时仍然会转成bool。
而qsort需要一个返回int类型的函数,正负分别表示大小关系,0表示相等。因此两者的效果是不一样的。我不知道你的题目要求,也没看你具体逻辑实现,但是可以肯定的是,了解了这两者的区别,是一定能搞明白这个程序为什么wrong answer的。