n块钱,2块钱一瓶啤酒,2个瓶子换一瓶,4个盖子换一瓶,能喝多少瓶

 

方法是递归一波,边界条件是不够钱,不够盖子,不够瓶子

#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;
} 

 

posted on 2017-11-17 12:02  任我主宰  阅读(326)  评论(0编辑  收藏  举报