问题描述
Problem C: FatMouse' Trade
Time Limit: 1 Sec Memory Limit:64 MB
Submit: 78 Solved:40
[Submit][Status][Web Board]
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
HINT
杭电1009
解题思路
这道题可以用贪心算法求解。
所以,在输入数据的同时,计算性价比 rate = J / F;
性价比越高的说明最后的收益越好。交换的时候就从性价比最高的开始(贪心的地方)。直到 M 换没。
这里我们先定义一个结构体 struct Mouse 类型来存储 J,F,和性价比 rate。
在交换之前,我们还要对性价比进行从大到小的排序。
代码
代码1:
#include <stdio.h>
#include <stdlib.h>
const int MAX = 1001;
struct Mouse
{
double j;
double f;
double rate;
} mouse[MAX];
int compare(const void *a, const void *b)
{
struct Mouse *c = (Mouse *)a;
struct Mouse *d = (Mouse *)b;
if (c->rate > d->rate)
return -1;
else
return 1;
}
int main()
{
int N;
double M;
double result;//result
while (scanf("%lf%d", &M, &N))
{
if (M == -1 && N == -1)
break;
//input
for (int i = 0; i < N; ++i)
{
scanf("%lf%lf", &mouse[i].j, &mouse[i].f);
mouse[i].rate = mouse[i].j / mouse[i].f;
}
qsort(mouse, N, sizeof(mouse[0]), compare);//stdlib.h 从大到小排序
result = 0;
for (int i = 0; i < N; ++i)
{
if (M >= mouse[i].f)
{
result += mouse[i].j;
M -= mouse[i].f;
}
else
{
result += (mouse[i].j/mouse[i].f)*M;
break;
}
}
printf("%.3lf\n", result);
}
return 0;
}
代码2:
#include <stdio.h>
#include <stdlib.h>
struct Mouse
{
double j;
double f;
double rate;
} mouse[1001];
int compare(const void *a, const void *b)
{
struct Mouse *c = (Mouse *)a;
struct Mouse *d = (Mouse *)b;
if (c->rate > d->rate)
return -1;
else
return 1;
}
int main()
{
int N;
double M;
double result;//result
while (scanf("%lf%d", &M, &N))
{
int i;
if (M == -1 && N == -1)
break;
//input
for (i = 0; i < N; ++i)
{
scanf("%lf%lf", &mouse[i].j, &mouse[i].f);
mouse[i].rate = mouse[i].j / mouse[i].f;
}
qsort(mouse, N, sizeof(mouse[0]), compare);//stdlib.h 从大到小排序
result = 0;
for (i = 0; i < N; ++i)
{
if (M >= mouse[i].f)
{
result += mouse[i].j;
M -= mouse[i].f;
}
else
{
result += (mouse[i].j/mouse[i].f)*M;
break;
}
}
printf("%.3lf\n", result);
}
return 0;
}
代码3:
#include<iostream>
#include<cstdlib>
#include <iomanip>
using namespace std;
struct Mouse // 结构体Mouse
{
int J;
int F;
};
int compare(const void*,const void*);
int main()
{
int M, N;
while(cin >> M >> N)
{
double sum = 0;
if(M == -1 && N == -1)
break;
Mouse *newone = (struct Mouse *)malloc(sizeof(struct Mouse)*N);// 动态分配内存
int i;
for(i = 0; i < N; ++i)
{
cin >> newone[i].J >> newone[i].F;
}
qsort(newone, N, sizeof(newone[0]), compare);
for (i = 0; i < N; ++i)
{
if (newone[i].F > M)
break;
sum += newone[i].J;
M = M - newone[i].F;
}
if (i < N)
sum += M * ((newone[i].J*1.0) / newone[i].F);
cout << setiosflags(ios::fixed) << setprecision(3) << sum << endl;//格式输出
}
return 0;
}
int compare(const void* c,const void* d)
{
Mouse *a=(Mouse *)c;
Mouse *b=(Mouse *)d;
if(a->J / (a->F*1.0) > b->J / (b->F*1.0))
return -1;
else if(a->J / (a->F*1.0) < b->J / (b->F*1.0))
return 1;
else
return 0;
}
总结
(1)qsort 排序的使用