Openjudge Exercise Solution (c + +)-title 4110: Santa Claus gift-santa Clau ' s Gifts

Source: Internet
Author: User

Problem:

Total time limit:
1000ms
Memory Limit:
65536kB
Describe

Christmas has come, in the city A in Santa Claus ready to distribute sweets, now has multiple boxes of different candies, each box of sweets has its own value and weight, each box of sweets can be split into any bulk combination to take away. Santa's reindeer can only bear a certain weight of candy, ask Santa Claus can take away much value of candy.

Input
The
first line consists of two parts, the number of candy box positive integer n (1 <= n <= 100), the maximum weight of the reindeer can withstand a positive integer w (0 < W < 10000), two numbers are separated by a space. The remaining n rows correspond to one box of candies per row, consisting of two parts, one for the value of a box of candies, a positive integer V and a weight positive integer w, separated by a space in the middle.
Output
output The maximum total value of candy that Santa Claus can take away, preserving 1 decimal places. The output is one line, ending with a newline character.
Sample input
4 15100 4412 8266 7591 2
Sample output
1193.0

Solution:

#include<stdio.h>#include<iostream>using namespaceStd;int Main(){intTimes;intWeight;intUsenu=0;DoubleAllvalue=0;intAllweight=0;Cin>>Times>>Weight;Usenu=Weight;struct MyGift{intValue;intWeit;DoublePerval;};struct MyGiftGift[Times];struct MyGiftTemp={0,0,0}; for(intI=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(intJ=0;J<Times;J++) {  for(intH=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(intZ=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 Exercise Solution (c + +)-title 4110: Santa Claus gift-santa Clau ' s Gifts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.