C ++中不浪费任何成分的汉堡数量
假设我们有两个整数TomatoSlices和cheeseSlices。这些是不同汉堡的成分-
巨型汉堡:4个番茄片和1奶酪片。
小汉堡:2个西红柿片和1奶酪片。
我们必须找到[total_jumbo,total_small],以使剩余的tomatoSlices的数量等于0,并且剩余的cheeseSlices的数量也为0。如果不可能使剩余的tomatoSlices和cheeseSlices等于0,则返回[]。因此,如果输入为tomatoSlices=16,而chesseSlices=7,则输出将为[1,6]。因此,这表明,要制作一个超大汉堡和6个小汉堡,我们需要4*1+2*6=16番茄切片和1+6=7奶酪切片。
为了解决这个问题,我们将遵循以下步骤-
制作一个称为ans的数组
如果番茄是奇数或奶酪>番茄/2或番茄>4*奶酪,则返回ans
x:=(4*奶酪-番茄)/2
y:=(番茄–(2*x))/4
将y然后将x插入数组ans
返回ans
让我们看下面的实现以更好地理解-
示例
#include <bits/stdc++.h> using namespace std; void print_vector(vector<auto> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector<int> numOfBurgers(int t, int c) { vector <int> ans; if(t % 2 != 0 || c > t/2 || t > c*4)return ans; int x = (4 * c - t) / 2; int y = ( t - (2 * x) )/ 4; ans.push_back(y); ans.push_back(x); return ans; } }; main(){ Solution ob; print_vector(ob.numOfBurgers(16,7)); }
输入值
16 7
输出结果
[1, 6, ]