current position:Home>A simple code to solve it

A simple code to solve it

2022-02-02 22:40:24 CSDN Q & A

use m An empty bottle can be exchanged for a coke , You bought it n Please output how many bottles of coke you can drink (n>m). Example input n=9,m=3. Output 13




Refer to the answer 1:

This is not leetcode Today's daily question ?

    //  Mathematical methods     public int numWaterBottles(int n, int m) {        int cnt = n / (m - 1);        return n % (m - 1) == 0 ? n + cnt - 1 : n + cnt;    }    //  Simulation     public int numWaterBottles(int n, int m) {        int cnt = n;        while (n >= m) {            cnt += n / m;            n = n / m + n % m;        }        return cnt;    }



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022240222989.html

Random recommended