http://pat.zju.edu.cn/contests/pat-b-practise/1020
分析: 贪心——按单价大者优先;
注意: 月饼库存量为int数据类型时,有一组数据不能通过;改为double,AC
代码:
<span style="font-size:24px;">//月饼
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
const int maxn=1002;
struct node
{
double num;
double price;
double v;
}a[maxn];
int cmp(node x, node y)
{
return x.v - y.v > 0.000001;
}
int main()
{
freopen("in.txt","r",stdin);
int n,w;
double ans=0;
scanf("%d%d",&n,&w);
for(int i=0;i<n;i++) scanf("%lf",&a[i].num);
for(int i=0;i<n;i++) scanf("%lf",&a[i].price);
for(int i=0;i<n;i++) a[i].v = a[i].price / a[i].num;
sort(a,a+n,cmp);
for(int i=0;i<n;i++){
if(w < a[i].num) {
ans += w*a[i].v;
break;
}
ans += a[i].price;
w -= a[i].num;
}
printf("%.2lf\n",ans);
return 0;
}
</span>