贪心算法(圣诞老人的礼物)百练4110 MOOC

#include<iostream>

#include<algorithm>

using namespace std;

const double eps=1e-6;

struct candy{

int v,w;

bool operator<(const candy&c)

{

return double(v)/w-double(c.v)/c.w>eps;

}

}candies[100];

int main()

{

int n,w;

cin>>n>>w;

for(int i=0;i<n;i++)

{

cin>>candies[i].v>>candies[i].w;

}

sort(candies,candies+n);

double totalv=0;

int totalw=0;

for(int i=0;i<n;i++)

{

if(totalw+candies[i].w<=w)

{

totalw+=candies[i].w;

totalv+=candies[i].v;

}

else

{totalv+=candies[i].v*(double(w-totalw)/candies[i].w);

break;}

}

printf(“%.1f”,totalv);

system(“pause”);

return 0;

}

    原文作者:贪心算法
    原文地址: https://blog.csdn.net/hhdmw/article/details/81042854
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞