OpenJudge百炼习题解答(C++)--题4110:圣诞老人的礼物-Santa Clau’s Gifts

Posted LazyChun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenJudge百炼习题解答(C++)--题4110:圣诞老人的礼物-Santa Clau’s Gifts相关的知识,希望对你有一定的参考价值。

题:

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

圣诞节来临了,在城市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;
 } 

以上是关于OpenJudge百炼习题解答(C++)--题4110:圣诞老人的礼物-Santa Clau’s Gifts的主要内容,如果未能解决你的问题,请参考以下文章

OpenJudge百炼习题解答(C++)--题4074:积水量

OpenJudge百炼习题解答(C++)--题4040:买书问题

OpenJudge百炼习题解答(C++)--题3142:球弹跳高度的计算

OpenJudge百炼习题解答(C++)--题4072:判断多个点是否在同一直线

OpenJudge百炼习题解答(C++)--题4045:与3和5无关的数

OpenJudge百炼习题解答(C++)--题4110:圣诞老人的礼物-Santa Clau’s Gifts