PAT 1070. Mooncake (25)

原创 2018年04月17日 17:17:48

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (<=1000), the number of different kinds of mooncakes, and D (<=500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:
3 200
180 150 100
7.5 7.2 4.5
Sample Output:

9.45

贪心算法,注意库存量和需求量的数据类型都是double型

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
struct cake
{
  double weight;
  double money;
  double price;
  cake(double w,double m)
  {
    weight = w;
    money = m;
    price = 1.0*money/weight;
  }
};
bool cmp(cake a,cake b)
{
  return a.price > b.price;
}
int main()
{
  int n;
  double m;
  cin>>n>>m;
  vector<cake> v;
  for(int i = 0; i < n; i++)
  {
    double a;
    cin>>a;
    v.push_back(cake(a,0));
  }
  for(int i = 0; i < n; i++)
  {
    double p;
    cin>>p;
    v[i].money = p;
    v[i].price = v[i].money*1.0/v[i].weight;
  }
  sort(v.begin(),v.end(),cmp);
  double maxProfit = 0;
  for(int i = 0; i < n && m > 0; i++)
  {
    if(v[i].weight < m)
    {
      maxProfit += v[i].money;
      m = m - v[i].weight;
    }
    else
    {
      maxProfit += m * v[i].price;
      m = 0;
    }
  }
  printf("%.2lf\n",maxProfit);
  return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hickey_Chen/article/details/79977751

【PAT】1070. Mooncake (25)

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
  • realxuejin
  • realxuejin
  • 2013-11-02 19:58:39
  • 1377

1070.Mooncake (25)

1070.Mooncake (25)pat-al-10702017-02-09 典型的贪心 乙级里也有这题 遇到段错误,可以考虑是不是数组越界,看看用到数组的地方是不是要对下标的判断更谨慎一点。坑见注...
  • ymdq1113
  • ymdq1113
  • 2017-02-09 17:35:14
  • 108

pat 1070Mooncake (25)

#include #include #include using namespace std; struct moon { double amounts; double pric...
  • cwzhsi
  • cwzhsi
  • 2014-10-21 14:34:11
  • 329

1070. Mooncake (25)【贪心】——PAT (Advanced Level) Practise

题目信息1070. Mooncake (25)时间限制100 ms 内存限制65536 kB 代码长度限制16000 BMooncake is a Chinese bakery product t...
  • xianyun2009
  • xianyun2009
  • 2015-12-27 23:03:26
  • 1124

PAT (Advanced Level) Practise 1070 Mooncake (25)

1070. Mooncake (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B ...
  • jtjy568805874
  • jtjy568805874
  • 2016-03-22 21:17:00
  • 264

PAT 1070. 结绳(25)-乙级

给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连。每次串连后,原来两段绳子的长度...
  • liuchuo
  • liuchuo
  • 2017-03-05 15:23:54
  • 1422

PAT-B 1020. 月饼(同PAT 1070. Mooncake)

给出n堆月饼的重量和总价 -- 可求每吨的均价 给出总共的购买量d, 求可获得的最大利润 很简单的。求均价后排降序,从均价最高的开始购入。 1. 基础知识不牢固,scan...
  • u014674776
  • u014674776
  • 2014-05-26 14:21:11
  • 767

PAT Basic 1070. 结绳(25)(C语言实现)

题目 给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连。每次串连后,原来两段绳...
  • Oliver__Lew
  • Oliver__Lew
  • 2017-05-18 23:47:30
  • 395

pat 1070. Mooncake (25)

1070. Mooncake (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...
  • wyh7280
  • wyh7280
  • 2015-07-19 11:21:49
  • 321

【PAT甲级】1070. Mooncake (25)

#include #include using namespace std; typedef struct Cake { double ton; double price; ...
  • lambsnow
  • lambsnow
  • 2017-06-21 17:25:13
  • 73
收藏助手
不良信息举报
您举报文章:PAT 1070. Mooncake (25)
举报原因:
原因补充:

(最多只允许输入30个字)