leecode | 不浪费原料制作汉堡问题

文章讲述了如何使用二元一次方程解决实际问题,即给定番茄片和奶酪片数量,确定巨无霸和小皇堡的制作方案,确保剩余原料为零。Solution类中的代码展示了计算方法和条件判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

纯属鸡兔同笼问题
解二元一次方层组

给你两个整数 tomatoSlices 和 cheeseSlices,分别表示番茄片和奶酪片的数目。不同汉堡的原料搭配如下:

巨无霸汉堡:4 片番茄和 1 片奶酪
小皇堡:2 片番茄和 1 片奶酪
请你以 [total_jumbo, total_small]([巨无霸汉堡总数,小皇堡总数])>的格式返回恰当的制作方案,使得剩下的番茄片 tomatoSlices 和奶酪片 cheeseSlices 的数量都是 0。

如果无法使剩下的番茄片 tomatoSlices 和奶酪片 cheeseSlices 的数量为 0,就请返回 []

class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        // 4x + 2y = tomatoSlices  ==> 2y = tomatoSlices - 4x
        // x + y = cheeseSlices     ==> 2x + tomatoSlices - 4x = 2*cheeseSlices
        // ==> 2x = tomatoSlices - 2*cheeseSlices
        vector<int> ans;
        int x = 0, y = 0;
        x = (tomatoSlices - 2*cheeseSlices)/2;
        y = cheeseSlices - x;
        // 4, 17 --> -15 32
        if(x < 0 || y < 0 || (tomatoSlices - 2*cheeseSlices)%2 !=0){
            return ans;
        }
        ans.push_back(x);
        ans.push_back(y);
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值