题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009

题意:一共有n个房子,每个房子里有老鼠喜欢吃的javabeans,但是每个房间里的javabeans的价格不一样。老鼠用m元,问m元最多可以卖多少javabeans,其中每个房间里的javabeans可以被分割。

直接求单价,排序即可

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<deque>
#include<sstream>
#include<cctype>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, s, t) for(int i = (s); i < (t); i++)
#define MEM(a, x) memset(a, x, sizeof(a));
#define DEBUG(x) cout<<x<<endl;
#define DBG cout<<"----------------"<<endl;
#define mid(x, y) x + (y - x)/ 2
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef int64_t INT;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;
const int maxn = 1e3 + ;
const double eps = 1e-;
const int INF = << ;
const int dir[][] = {,,,,,-,-,};
const double pi = 3.1415926535898;
int T, n, m, cases;
struct node
{
double j, f;
bool operator < (const node& a)const
{
return (j / f) > (a.j / a.f);
}
};
node a[maxn];
int main()
{
while(cin >> m >> n)
{
if(m == - && n == -)break;
memset(a, , sizeof(a));
for(int i = ; i < n; i++)
{
cin >> a[i].j >> a[i].f;
}
sort(a, a + n);
double ans = ;
for(int i = ; i < n; i++)
{
if(m >= a[i].f)
{
ans += a[i].j;
m -= a[i].f;
}
else
{
ans += (m * a[i].j / a[i].f);
break;
}
}
printf("%.3f\n", ans);
}
return ;
}
04-30 23:25