2739. 总行驶距离

时间:2024-04-25 19:51:56
思路

模拟一下即可,每次让 mainTank 减去 5 ,然后如果 additionalTank 大于 0,就在让 mainTank 加 1,最后让 additionalTank 减 1。当不满足 mainTank - 5 >= 0 && additionalTank > 0 时即退出。

class Solution {
public:
    int distanceTraveled(int mainTank, int additionalTank) {
        int res = mainTank * 10;
        while ( mainTank ) {
            if ( mainTank - 5 >= 0 && additionalTank > 0 ) {
                res += 10;
                mainTank -= 5;
                mainTank ++;
                additionalTank --;
            }
            else break;
        }
        return res;
    }
};