Greedy Mouse
-
描述
-
A fat mouse prepared M pounds of cat food,ready to trade with the cats guarding the warehouse containing his
favorite food:peanut. The warehouse has N rooms.The ith room containsW[i] pounds of peanut and requires
F[i] pounds of cat food. Fatmouse does not have to trade for all the peanut in the room,instead,he may get
W[i]*a% pounds of peanut if he pays F[i]*a% pounds of cat food.The mouse is a stupid mouse,so can you tell
him the maximum amount of peanut he can obtain.
-
输入
- 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 W[i] and F[i] respectively. The test case is terminated by two -1. All integers are not greater than 1000. 输出
- For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of penaut that FatMouse can obtain. 样例输入
-
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
样例输出
-
13.333 31.500
上传者
题目描述:
从前有只肥肥的老鼠,他叫FatMouse,他就像人类的恐怖分子跟敌人交易军火一样,
猥琐的他准备了M磅猫食,准备与守卫仓库的大猫们进行交易,仓库里有他最爱吃的食物Javabean。
仓库里有N个房间,第i间房间里有J[i]磅Javabean且需要F[i]磅猫食进行交换,FatMouse不必吧
每个房间里的Javabean全部用于交易,
相反,他可以付给大猫F[i]*a%磅猫食,从而换的J[i]*a%磅的Javabean。
其中,a是一个实数,现在他给你布置一个家庭作业,
请你告诉他他最多能够获得多少磅Javabean。
输入描述:
输入包含多组测试数据,每组测试数据的开头一行是两个非负整数M, N.接下来的N行中,
每行包含两个非负整数J[i]和F[i],最后一组测试数据是两个-1,所有的整数的值不糊超过1000;
输出描述:
对于每组测试数据,在一行上打印出一个3位小数的实数,
这个实数是FatMouse能够交易到的最大数量的Javabean.
double in[100];
int cmp( const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
}
qsort(in,100,sizeof(in[0]),cmp);
思路:用M的猫粮来购买最多的Javabean的问题。跟背包问题类似。
1.计算一份猫粮换的JavaBean量 ,即J [ i ] / F [ i ],并对其降序排序。
2.优先换 J [ i ] / F [ i ] 数值大的,可以换更多。
3.这题跟a%并没有关系,不要执着于a%。
4,有用到qsort对double类型数组排序,要特别注意,只能用三目运算符!!!否则用 “ - ”的话,数值默认为int型,会出错。cmp的返回值是int型,因此会将double这个小数返回0,系统认为是相等,失去了本来存在的大小关系。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct mouse{
double f,j,array;//cat food ,JavaBean,一份猫粮换的JavaBean
}mou[200];
int cmp(const void *a,const void *b){
return (*(mouse *)b).array>(*(mouse *)a).array?1:-1;
}//降序
int main(){
int n,i;
double m,jsum;
while(scanf("%lf%d",&m,&n)&&m!=-1&&n!=-1){
memset(mou,0,sizeof(mou));
for(i=0;i<n;i++){
scanf("%lf%lf",&mou[i].j,&mou[i].f);
mou[i].array=mou[i].j/mou[i].f;//一份猫粮换的JavaBean量
}
qsort(mou,n,sizeof(mou[0]),cmp);
/*********以下为计数********/
jsum=0;
for(i=0;i<n;i++){
if(m>=mou[i].f){
m-=mou[i].f;
jsum+=mou[i].j;
}
else if((m<mou[i].f)&&m>=0){
jsum+=m*mou[i].array;
break;
}
}
printf("%.3lf\n",jsum);
}
return 0;
}
用sort更简单:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct mou{
double j,c,array;
}f[1010];
double cmp(const mou &a,const mou &b){
return a.array>b.array;
}
int main(){
int i,n;
double sum,m;
while(scanf("%lf%d",&m,&n)&&m!=-1&&n!=-1){
memset(f,0,sizeof(f));
for(i=0;i<n;i++){
scanf("%lf%lf",&f[i].j,&f[i].c);
f[i].array=f[i].j/f[i].c;
}
sort(f,f+n,cmp);
sum=0;
for(i=0;i<n;i++){
if(m>=f[i].c){ //=是需要的
sum+=f[i].j;
m-=f[i].c;
}
else if((m<f[i].c)&&m>=0){ //=是需要的
sum+=(m*f[i].array);
break;
}
}
printf("%.3lf\n",sum);
}
return 0;
}