Cover image for leetcode每日一题 P1518 换水问题

leetcode每日一题 P1518 换水问题

字数 99
阅读
访客

时间轴

时间轴

2025-10-01

init

数学,模拟

题目:

简单的数学模拟题

12345678910111213141516171819
#include <stdio.h>class Solution {public:  int numWaterBottles(int numBottles, int numExchange) {    // 每numExchange个bottles喝完可以换一瓶bottle    int exchange_bootles = -1;    int total_water = 0;    do {      exchange_bootles = numBottles / numExchange;      numBottles = numBottles % numExchange;      numBottles += exchange_bootles;      total_water += exchange_bootles * numExchange;    } while (numBottles >= numExchange);    total_water += numBottles;    return total_water;  }};
评论加载中…