97 equivalent Exchange (Greedy-2)

Source: Internet
Author: User

Description
Heilongjiang Wuchang Rice is famous all over the country, every year to autumn, farmers put their own rice to the market to buy, but because the Wuchang area is still a relatively backward place, but also the implementation of barter, that is, farmers with rice for flour, can be used to steamed buns Ah! The proportion of rice flour in each market is not equal, how can you use the least rice for the most flour? (Unit is catty)
Input
There are several groups of input data, the first row of each group of data has 2 numbers: M and n,m represents the number of pounds of rice, n is the n market, followed by n lines, 2 numbers per line T1 and T2, said in this market can be used T1 Jin Rice for T2 pound of white flour (only t2 pound of white flour).
Output
Output m-pound rice can be changed to the maximum value of white flour (results retain 2 decimal places)
Sample Input
5 31 24) 91 5
Sample Output
14.00
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <stdio.h>5#include <math.h>6 using namespacestd;7 structSA8 {9     intT1;Ten     intT2; One     DoubleAWP; A}data[ +]; - DoublecmpConstSA &a,ConstSA &b) - { the     returnA.awp>B.AWP; - } - intMain () - { +     Doubleans; -     intn,m; +      while(cin>>n>>m) A     { at          for(intI=0; i<m;i++) -         { -Cin>>data[i].t1>>data[i].t2; -data[i].awp=1.0*data[i].t2/data[i].t1; -         } -Sort (data,data+m,cmp); inans=0.0; -          for(intI=0; i<m;i++) to         { +             if(n>=data[i].t1) -             { theans+=data[i].t2; *n=n-data[i].t1; $             }Panax Notoginseng  -             Else the{ANS+=N*DATA[I].AWP; Break;} +         } Aprintf"%.2lf\n", ans); the     } +     return 0; -}
View Code

Note You must jump out of the for loop after else

97 equivalent Exchange (Greedy-2)

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.