“换汽水”问题(用C++)

算法描述:三个空汽水瓶可以换一瓶汽水。小董有10个空汽水瓶,他最多可以换多少瓶汽水喝?答案:5瓶。

方法如下:先用9个空瓶子换3瓶汽水,然后把这3瓶喝掉以后就有4个空瓶子。然后再用3个换一瓶满的,这时手上有2个空瓶子。然后他再向老板借了一瓶汽水,喝掉这瓶满的,用3个空瓶子换了一瓶满的换个老板。如果小董手上有n个空汽水瓶,最多可以换多少汽水喝?(该代码只测试单个输入,不测试数组)

#include<stdio.h>
#include"iostream"
using namespace std;

int Change(int x) {
    int count=0;
    int a;   //整数
    int b;    //余数
    int c;
    if (x <= 1) {
    
        return 0;

    } if(x==2) {
        count++;
        return count;
    }else if(x >= 3) {
        
        a = x / 3;
        b = x % 3;
        count =a + Change((x / 3)+(x%3));
        
    }
    return count;
}

int main() {
    int buy;
    int total;
    cin >> buy;
    total = Change(buy);
    cout << "cant change " << total << endl;
    system("pause");
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值