贪心算法——PAT 1020 月饼

1020 月饼(25 分)

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。

输入格式:

每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。

输出格式:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。

输入样例:

3 20
18 15 10
75 72 45

输出样例:

94.50

 

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

struct mooncake{
	int stock;//每种月饼的库存量
	int totalPrice;//每种月饼的总售价
	double price;//每种月饼的单价
};
int findMax(mooncake *c, int n)
{
	double max;
	int index = 0;
	max = c[0].price;
	for (int i = 0; i < n; i++)
	{
		if (max < c[i].price && c[i].stock > 0)
		{
			max = c[i].price;
			index = i;
		}
	}
	return index;
}
int min(int a, int b)
{
	return a < b ? a : b;
}

int main()
{
	int n;//月饼种类
	int d;//市场最大需求量
	mooncake cake[1001];
	double sale = 0;//一共卖出的总价
	int temp;
	
	cin >> n >> d;
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		cake[i].stock = temp;
	}
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		cake[i].totalPrice = temp;
		cake[i].price = cake[i].totalPrice / double(cake[i].stock);
	}

	int num;//月饼卖出的数量
	int index;//单价最高的月饼编号
	while (d != 0)
	{
		index = findMax(cake, n);
		num = min(d, cake[index].stock);
		sale += cake[index].price * num;
		cake[index].stock -= num;
		d -= num;
	}
	cout << setiosflags(ios::fixed) << setprecision(2) << sale;
	system("pause");
	return 0;
}

 

 

 

把cin>>temp;改为  scanf("%d", &temp); 运行时间缩短了

 

 

 

把寻找单价最大值改为对单价从大到小进行排序sort

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

struct mooncake{
	int stock;//每种月饼的库存量
	int totalPrice;//每种月饼的总售价
	double price;//每种月饼的单价
};
int cmp(mooncake a, mooncake b)
{
	return a.price > b.price;
}
int min(int a, int b)
{
	return a < b ? a : b;
}

int main()
{
	int n;//月饼种类
	int d;//市场最大需求量
	mooncake cake[1001];
	double sale = 0;//一共卖出的总价
	int temp;

	cin >> n >> d;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &temp);
		cake[i].stock = temp;
	}
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &temp);
		cake[i].totalPrice = temp;
		cake[i].price = cake[i].totalPrice / double(cake[i].stock);
	}

	sort(cake, cake + n, cmp);//月饼按单价排序

	int num;//月饼卖出的数量
	int index = 0;//单价最高的月饼编号
	while (d != 0)
	{
		num = min(d, cake[index].stock);
		sale += cake[index].price * num;
		cake[index].stock -= num;
		d -= num;
		index++;//如果该月饼库存不为0.d肯定为0
	}
	cout << setiosflags(ios::fixed) << setprecision(2) << sale;
	system("pause");
	return 0;
}

 

 

把 库存量stock、总售价totalPPrice、temp改为double类型(可能是精度的原因)

AC~~~

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

struct mooncake{
	double stock;//每种月饼的库存量
	double totalPrice;//每种月饼的总售价
	double price;//每种月饼的单价
};
int cmp(mooncake a, mooncake b)
{
	return a.price > b.price;
}
int min(int a, int b)
{
	return a < b ? a : b;
}

int main()
{
	int n;//月饼种类
	int d;//市场最大需求量
	mooncake cake[1001];
	double sale = 0;//一共卖出的总价
	double temp;

	cin >> n >> d;
	for (int i = 0; i < n; i++)
	{
		scanf("%lf", &temp);
		cake[i].stock = temp;
	}
	for (int i = 0; i < n; i++)
	{
		scanf("%lf", &temp);
		cake[i].totalPrice = temp;
		cake[i].price = cake[i].totalPrice / cake[i].stock;
	}

	sort(cake, cake + n, cmp);//月饼按单价排序

	int num;//月饼卖出的数量
	int index = 0;//单价最高的月饼编号
	while (d != 0)
	{
		num = min(d, cake[index].stock);
		sale += cake[index].price * num;
		cake[index].stock -= num;
		d -= num;
		index++;//如果该月饼库存不为0.d肯定为0
	}
	cout << setiosflags(ios::fixed) << setprecision(2) << sale;
	system("pause");
	return 0;
}

 

也可以:

vector<mooncake> a(n);

printf("%.2f",result); 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章