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
廣告