码迷,mamicode.com
首页 > 其他好文 > 详细

2015-基础(3)

时间:2015-02-04 14:47:14      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:acm

C - C

Description

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. 
 

Sample Input

5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
 

Sample Output

13.333 31.500
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
int a[5000],b[5000];
int main()
{
	int t,n,i,j;
	double c[5000],k,sum;
	while(cin >> t >> n)
	{
		sum=0;
		if(t==-1&&n==-1)
		{
			break;
		}
		for(i=0;i<n;i++)
		{
			cin >> a[i] >> b[i];
			c[i]=a[i]*1.0/b[i];
		}
		for(i=0;i<n-1;i++)
		{
			for(j=i+1;j<n;j++)
			{
				if(c[i]<c[j])
				{
					k=c[i];
					c[i]=c[j];
					c[j]=k;
					k=b[i];
					b[i]=b[j];
					b[j]=k;
					k=a[i];
					a[i]=a[j];
					a[j]=k;
				}
			}
		}
		for(i=0;i<n;i++)
		{
			if(b[i]<t)
			{
				sum+=a[i];
				t-=b[i];
				c[i]=0;
			}
			else
			{
				sum+=t*1.0*c[i];
				break;
			}
		}
		cout << setiosflags(ios::fixed) << setprecision(3) << sum << endl;
	}
	return 0;
}


2015-基础(3)

标签:acm

原文地址:http://blog.csdn.net/zsc2014030403015/article/details/43484513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!