瓶子蓋子換啤酒

n塊錢,2塊錢一瓶啤酒,2個瓶子換一瓶,4個蓋子換一瓶,能喝多少瓶ios

 

方法是遞歸一波,邊界條件是不夠錢,不夠蓋子,不夠瓶子spa

#include<iostream>
using namespace std;
int buy(int money,int g,int p,int count){

    if(money < 2 && g< 4 && p<2) return count;
    else{
        if(money>=2){
            count += money/2;
            g += money/2;
            p += money/2;
            money = money%2;
        }
        if(g>=4){
            count += g/4;
            p += g/4;
            int t=g;
            g = g%4;
            g += t/4;        
        }
        if(p>=2){
            count += p/2;
            g += p/2;
            int t=p;
            p = p%2;
            p += t/2;
        }
    }
    buy(money,g,p,count);
    
}

int main(){
    int n;
    while(cin>>n)
        cout<<buy(n,0,0,0)<<endl;
} 
相關文章
相關標籤/搜索