FatMouse's Trade【九度教程第21題】

題目描述:

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.

輸入:

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.

輸出:

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.

樣例輸入:

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

樣例輸出:

13.333
31.500

解釋題目

題目大概可以化爲用固定的錢買東西,求一種方案使得買到東西獎勵最大。
一共有M元錢,有N種商品可供購買,每種商品個數僅有一個,且每種商品都有一個固定的獎勵(reward),如何花錢使得買到商品的獎勵最大?(無法買的起的話 按照剩餘錢給相應的獎勵)

貪心策略

貪心算法實際上就是一個追求性價比的過程,這道題目裏的性價比就是每種商品的獎勵/商品的價格,當這個性價比越大,我們就越應該買它。所以這道題的貪心策略就是選擇性價比最高的買

代碼

根據之前刷的幾道題,代碼基本的框架流程應該很清晰了,重要包括

  1. 控制檯多組輸入?while (scanf("%d", &n) != EOF)
  2. 每行什麼結構存儲?struct

然後這道題裏比較重要的點在:

  1. 先算每個商品的性價比,然後對性價比進行排序 sort函數
  2. 額外的約束退出條件在哪添加?
#define _CRT_SECURE_NO_WARNINGS // 用來解除scanf的安全檢查 或者 手動設置項目屬性取消SDL檢查
#include<stdio.h>
#include<algorithm>

using namespace std;
struct goods {
	double f; //商品獎勵
	double j; //商品價格
	double s; //商品性價比
}buf[1000];
bool cmp(goods a, goods b) {//定義規則 按照從大到小的順序排列
	return a.s > b.s;
}

int main(){
	double m;
	int n;
	while (scanf("%lf%d", &m, &n) != EOF) { //常規
		if (m == -1 && n == -1) //額外判斷
			break;
		for (int i = 0; i < n; i++) {
			scanf("%lf%lf", &buf[i].f, &buf[i].j);
			buf[i].s = buf[i].f / buf[i].j;
		}
		sort(buf, buf + n, cmp);
		int idx = 0;
		double ans = 0;
		while (m > 0 && idx < n) {
			if (m > buf[idx].j) {// 剩餘錢可以買目標商品
				ans += buf[idx].f;
				m -= buf[idx].j;
				idx++;
			}
			else { //剩餘錢買不起所有目標商品,根據題意 按比例得到獎勵
				ans += buf[idx].f * m / buf[idx].j;
				m = 0;// 沒錢了 結束了
			}
		}
		printf("%.3lf", ans);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章