题目:http://pat.zju.edu.cn/contests/pat-a-practise/1070
题解:
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct moonCake
{
double n;
double price;
}cake[2005];
bool cmp(const struct moonCake &a,const struct moonCake &b)
{
return a.price>b.price;
}
int main()
{
int n;
double summ,getx=0;
scanf("%d%lf",&n,&summ);
for(int i=0;i<n;++i)
scanf("%lf",&cake[i].n);
for(int i=0;i<n;++i)
{
scanf("%lf",&cake[i].price);
if(cake[i].n>0)
{
cake[i].price=cake[i].price/cake[i].n;
}
else
cake[i].price=0;
}
sort(cake,cake+n,cmp);
for(int i=0;i<n&&summ>0;++i)
{
if(summ>=cake[i].n)
{
getx+=(cake[i].price*cake[i].n);
summ-=cake[i].n;
}
else
{
getx+=(summ*cake[i].price);
summ=0;
}
}
printf("%.2f\n",getx);
return 0;
}
来源: http://blog.csdn.net/acm_ted/article/details/20147861