测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A48212. 圣诞老人的礼物圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成任意散装组合带走。圣诞老人的驯鹿最多只能承受一定重量的糖果,请问圣诞老人最多能带走多大价值的糖果。输入第一行由两个部分组成,分别为糖果箱数正整数n(1 <= n <= 100),驯鹿能承受的最大重量正整数w(0 < w < 10000),两个数用空格隔开。其余n行每行…

填空题 困难

题目描述

圣诞老人的礼物

圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成任意散装组合带走。圣诞老人的驯鹿最多只能承受一定重量的糖果,请问圣诞老人最多能带走多大价值的糖果。

输入

第一行由两个部分组成,分别为糖果箱数正整数n(1 <= n <= 100),驯鹿能承受的最大重量正整数w(0 < w < 10000),两个数用空格隔开。其余n行每行对应一箱糖果,由两部分组成,分别为一箱糖果的价值正整数v和重量正整数w,中间用空格隔开。

输出

输出圣诞老人能带走的糖果的最大总价值,保留1位小数。输出为一行,以换行符结束。

样例输入

4 15

100 4

412 8

266 7

591 2

样例输出

1193.0

参考答案

#include<stdio.h> #include<iostream> using namespace std; int main() { int times; int weight; int UseNu=0; double AllValue=0; int AllWeight=0; cin>>times>>weight; UseNu=weight; struct MyGift{ int Value; int Weit; double perVal; }; struct MyGift Gift[times]; struct MyGift Temp={0,0,0 }; for(int i=0;i<times;i++) { cin>>Gift[i].Value>>Gift[i].Weit; Gift[i].perVal=(double)(Gift[i].Value)/(double)(Gift[i].Weit); AllWeight+=Gift[i].Weit; AllValue+=Gift[i].Value; } for(int j=0;j<times;j++) { for(int h=1;h<times;h++) { if(Gift[h-1].perVal<Gift[h].perVal) { Temp=Gift[h-1]; Gift[h-1]=Gift[h]; Gift[h]=Temp; } } } if(AllWeight<=weight) { printf("%0.1f\n",(double)(AllValue)); return 0; } AllValue=0; for(int z=0;z<times;z++) { if(Gift[z].Weit<=UseNu) { AllValue+=Gift[z].Value; UseNu-=Gift[z].Weit; } else{ AllValue+=Gift[z].perVal*UseNu; printf("%0.1f\n",AllValue); return 0; } } return 0; }
上一题 下一题