Javabean(MAX)

// Z_1009.cpp : 定义控制台应用程序的入口点。
//
/*
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.



Input
The input consists of multiple test cases.Each test case begins with a line containing two non - negative integers M and N.Then N lines follow, each contains two non - negative integers J[i] and F[i] respectively.The last test case is followed by two - 1's. All integers are not greater than 1000.



Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
*/




#include<iostream>
#include <iomanip> 
using namespace std;

int** input()
{
	int M, N,**bank;
	cin >> M >> N;
	if (M == -1 && N == -1)
		return NULL;
	bank = new int*[2];
	bank[0] = new int[N+1];
	bank[1] = new int[N+1];
	bank[0][0] = M;
	bank[1][0] = N;
	for (int i = 1; i < N+1; i++)
	{
		cin >> bank[0][i];
		cin >> bank[1][i];
	}
	return bank;
}

int** sort(int **bank)
{
	for (int i = 2; i < bank[1][0] + 1; i++)
	{
		for (int j = 1; j < bank[1][0] + 2 - i; j++)
		{
			//cout << (double)bank[0][j] / bank[1][j] << endl;
			//cout << bank[0][j + 1] / bank[1][j + 1] << endl;
			if ((double)bank[0][j] / bank[1][j] < (double)bank[0][j + 1] / bank[1][j + 1])
			{
				//cout << "change\n";
				int x = bank[0][j]; bank[0][j] = bank[0][j + 1]; bank[0][j + 1] = x;
				int y = bank[1][j]; bank[1][j] = bank[1][j + 1]; bank[1][j + 1] = y;
			}
			//cout << 'i' << i << '\t' << 'j' << j << endl;
		}
	}
	return bank;
}
double workout(int **bank)
{
	double sum = 0;
	for (int i = 1; i < bank[1][0]+1; i++)
	{
		if (bank[0][0] - bank[1][i] >= 0)
		{
			sum += bank[0][i];
			bank[0][0] -= bank[1][i];
		}
		else
		{
			sum += bank[0][i] * (double)bank[0][0] / bank[1][i];
			break;
		}
	}
	return sum;
}
void output(int **bank)
{
	for (int i = 0;i<bank[1][0]+1; i++)
	{
		cout << bank[0][i] <<'\t';
		cout << bank[1][i] <<'\n';
	}
}
int main()
{
	double ans[256] = { 0 };
	int **bank, i = 0;
	while (true)
	{
		bank = input();
		if (bank == NULL)
			break;
		ans[i] = workout(sort(bank));
		i++;
	}
	for (int i = 0;; i++)
	{
		if (ans[i] == 0)
			break;
		cout << fixed << setprecision(3) << ans[i] << endl;
	}

}

posted @ 2015-07-19 12:05  A_yes  阅读(123)  评论(0编辑  收藏  举报