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)