#include<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
double num,profit;
}a[1050];
bool cmp(node a,node b)
{
return a.profit/a.num > b.profit/b.num;
}
int main()
{
int type,total;
int i;
scanf("%d%d",&type,&total);
for(i = 0;i < type;i++)
{
scanf("%lf",&a[i].num);
}
for(i = 0;i < type;i++)
{
scanf("%lf",&a[i].profit);
}
sort(a,a+type,cmp);
double sum = 0,sell = 0;
for(i = 0;i < type;i++)
{
if(total >= a[i].num)
{
sell = a[i].num;
total -= a[i].num;
sum += a[i].profit;
}
else
{
sell = total;
sum += sell * (a[i].profit/a[i].num) ;
break;
}
}
printf("%.2f\n",sum);
return 0;
}
#include<algorithm>
using namespace std;
struct node
{
double num,profit;
}a[1050];
bool cmp(node a,node b)
{
return a.profit/a.num > b.profit/b.num;
}
int main()
{
int type,total;
int i;
scanf("%d%d",&type,&total);
for(i = 0;i < type;i++)
{
scanf("%lf",&a[i].num);
}
for(i = 0;i < type;i++)
{
scanf("%lf",&a[i].profit);
}
sort(a,a+type,cmp);
double sum = 0,sell = 0;
for(i = 0;i < type;i++)
{
if(total >= a[i].num)
{
sell = a[i].num;
total -= a[i].num;
sum += a[i].profit;
}
else
{
sell = total;
sum += sell * (a[i].profit/a[i].num) ;
break;
}
}
printf("%.2f\n",sum);
return 0;
}
这道题一定要考虑全面,我当时就是遗漏了供不应求的情况导致段错误,这个一定要注意,浪费了我好长时间的。