# Using C + + to complete coke counting applet

2022-01-27 03:06:17

Title Description ：
Xiao Ming has n A bottle of coke , Every time he drinks a bottle of coke, he saves the cap ,k(k>1) A bottle cap can replace a new coke , So how much coke can Xiao Ming finally drink ？
Input format ：
The input contains multiple sets of samples , The first line contains a number T Indicates the number of sample groups
After that, each sample contains two integers n,k, The meaning is as described in the title
Output format ：
Each sample outputs a number num Indicates the number of cokes you can drink
sample input ：
2
3 2
4 4
sample output ：
5
5

``#include <iostream>using namespace std;int main(){    int res = 0, T, n, k;    cin >> T;    while (T--)    {        cin >> n >> k;        res = n; // There must be at the beginning n bottle         while (n >= k)        {            res += n / k;            n = n / k + n % k;        }        cout << res << endl;    }    return 0;}``

``#include <iostream>#include <cstdint>using namespace std;uint32_t cokes(uint32_t n, uint32_t k){    uint32_t num = n; /*!<  Total cokes  */    uint32_t caps = n;/*!<  Number of remaining caps  */    while (caps >= k) {        num += caps / k;        caps = (caps / k) + (caps % k);    }    return num;}int main(int argc, char **argv){    uint32_t n, k;    while (1) {        cin >> n >> k;        if (k <= 1) {            cout << "Invalid exchange rate " << k << "Must bigger than 1" << endl;        } else {            cout << cokes(n, k) << endl;        }    }}``