C++ program to find maximum profit we can make by making hamburger and chicken burgers

C++Server Side ProgrammingProgramming

Suppose we have five numbers b, p, f, h and c. There are two types of burgers in a restaurant. These are hamburger and chicken burger. Hamburger needs two buns and a beef patty and for chicken burger we need two buns and a chicken cutlet. We have b buns, p beef patties, f chicken cutlets. We are trying to sell hamburger for h rupees and chicken burger for c rupees. We have to find the maximum profit that we can gain.

So, if the input is like b = 7; p = 5; f = 2; h = 10; c = 12, then the output will be 34, because for one hamburgers and two chicken burgers. The income is 1*10 + 2*12 = 34.

Steps

To solve this, we will follow these steps −

res := 0
b := b / 2
if h < c, then:
   swap p and f
   swap h and c
res := res + h * (minimum of b and p) + c * minimum of the (maximum of (b - p) and 0) and f)
return res

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

int solve(int b, int p, int f, int h, int c) {
   int res = 0;
   b /= 2;
   if (h < c) {
      swap(p, f);
      swap(h, c);
   }
   res += h * min(b, p) + c * min(max(b - p, 0), f);
   return res;
}
int main() {
   int b = 7;
   int p = 5;
   int f = 2;
   int h = 10;
   int c = 12;
   cout << solve(b, p, f, h, c) << endl;
}

Input

7, 5, 2, 10, 12

Output

34
raja
Updated on 03-Mar-2022 06:20:46

Advertisements