-
Notifications
You must be signed in to change notification settings - Fork 0
/
12GreedFatMouse1433.cpp
57 lines (53 loc) · 1.25 KB
/
12GreedFatMouse1433.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
class goods
{
public:
double f;//finance :money:cat's Food
double p;//goods'pound:JavaBean
double c;//j/m:cost performance
bool operator <(const goods& g)const
{
return c>g.c;
}
} buff[1000];
int main()
{
double m;
int n;//m:money;n:n house/chose
while(cin>>m)
{
cin>>n;
if(m==-1&&n==-1)
break;
else
{
for(int i=0; i<n; i++)
{
cin>>buff[i].p>>buff[i].f;
buff[i].c=buff[i].p/buff[i].f;
}
sort(buff,buff+n);
int index=0;//current home index;
double sum=0;//obtain goods;
while(m>0&&index<n)
{
if(m>buff[index].f)//can buy total goods
{
sum+=buff[index].p;
m-=buff[index].f;
}
else//use remain money to buy goods
{
sum+=m*buff[index].p/buff[index].f;
m=0;
}
index++;//buy next house'goods
}
cout<<fixed<<setprecision(3)<<sum<<endl;
}
}
return 0;
}