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

2023-12-25 09:52:26

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

给你两个整数 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;
    }
};

文章来源:https://blog.csdn.net/ttxiaoxiaobai/article/details/135190742
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。