C++程序,用于查找通过制作汉堡和鸡肉汉堡可以获得的最大利润
假设我们有五个数字b、p、f、h和c。一家餐厅提供两种类型的汉堡。分别是汉堡和鸡肉汉堡。汉堡需要两个面包和一个牛肉饼,而鸡肉汉堡需要两个面包和一个鸡肉排。我们有b个面包、p个牛肉饼、f个鸡肉排。我们试图以h卢比的价格出售汉堡,以c卢比的价格出售鸡肉汉堡。我们必须找到我们可以获得的最大利润。
因此,如果输入类似于b = 7;p = 5;f = 2;h = 10;c = 12,则输出将为34,因为一个汉堡和两个鸡肉汉堡。收入为1*10 + 2*12 = 34。
步骤
为了解决这个问题,我们将遵循以下步骤 -
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
示例
让我们看看以下实现以获得更好的理解 -
#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;
}输入
7, 5, 2, 10, 12
输出
34
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP