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);