东方博宜OJ 1376:买汽水 ← 递归

【题目来源】
https://oj.czos.cn/p/1376

【题目描述】
一瓶饮料 n 元钱,两个空瓶换一瓶,有 m 元最多可以喝几瓶?

【输入格式】
两个整数 n,m(1≤n<m≤100000)​​​​​​​

【输出格式】
最多可以喝到的瓶数。

【输入样例】
2 10

【输出样例】
9

【数据范围】
1≤n<m≤100000

【算法分析】
● 模拟分析过程如下
(1)输入:n=2,m=6
(2)计算过程:
初始购买:6 / 2 = 3 瓶 → 喝完得 3 个空瓶。
兑换:3 / 2 = 1 瓶 → 喝完得 1 个空瓶 + 剩余 1 个空瓶 = 2 个空瓶。
兑换:2 / 2 = 1 瓶 → 喝完得 1 个空瓶。
无法继续兑换(剩余 1 个空瓶 < 2)。
总计:3 + 1 + 1 = 5 瓶。

【算法代码】

#include <bits/stdc++.h>
using namespace std;

int cal(int x) {
    if(x<2) return 0;
    int t=x/2;
    return t+cal(t+x%2);
}

int main() {
    int n,m;
    cin>>n>>m;
    int cnt=m/n;
    cout<<cnt+cal(cnt)<<endl;
    return 0;
}

/*
in:2 10
out:9
*/





【参考文献】
https://oj.czos.cn/p/1376
https://blog.csdn.net/hnjzsyjyj/article/details/156206151
https://blog.csdn.net/hnjzsyjyj/article/details/156204715



 

posted @ 2025-12-24 03:57  Triwa  阅读(0)  评论(0)    收藏  举报