21-题目1433:FatMouse

65 篇文章 0 订阅
53 篇文章 0 订阅

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值