Cover image for leetcode每日一题 P3100 换水问题II

leetcode每日一题 P3100 换水问题II

字数 103
阅读
访客

时间轴

时间轴

2025-10-02

init

数学,模拟

题目:

总是喝完所有水,如果没水就交换

123456789101112131415161718192021222324
class Solution {public:  int maxBottlesDrunk(int numBottles, int numExchange) {    int full_bottles = numBottles;    int empty_bottles = 0;    int num_exchange = numExchange;    int bottle_drunk = 0;    while (full_bottles > 0 || empty_bottles >= num_exchange) {      // drink      bottle_drunk += full_bottles;      empty_bottles += full_bottles;      full_bottles = 0;      // exchange      while (empty_bottles >= num_exchange) {        empty_bottles -= num_exchange;        num_exchange++;        full_bottles++;      }    }    return bottle_drunk;  }};
评论加载中…