http://ac.jobdu.com/problem.php?pid=1433
题目描述:
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
题目不难,就是看英文麻烦。。。。。
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include <iomanip> //小数点对齐
using namespace std;
typedef struct Room
{
double food;
double price;
}Room;
bool cmp(Room a, Room b) //按房间的单价降序排列
{
return a.food / a.price > b.food / b.price;
}
int main()
{
int M, N, i;
//ifstream cin("data.txt");
while (cin >> M >> N && M != -1)
{
double sum = 0, cost= 0;
Room * room = new Room[N];
for (i = 0; i < N; i++)
cin >> room[i].food >> room[i].price;
sort(room, room + N, cmp); // 按单价将房间排序
for (i = 0; i < N && cost < M; i++)
{
if (room[i].price <= M - cost)
{
cost += room[i].price;
sum += room[i].food;
}
else
{
sum += (M - cost)*(room[i].food / room[i].price);
cost = M;
}
}
cout << setiosflags(ios::fixed) << setprecision(3) << sum << endl; //精确到输出小数点后三位
}//end of while
//system("pause");
return 0;
}