A beer on the market, 2 yuan per bottle, 4 caps can be exchanged for a bottle of beers, two empty beer can also be exchanged for a bottle of beer. How many bottles can I drink when I finish entering money?
1#include <stdio.h>2 3 voidMain ()4 {5 intMoney,bottle_top,empty_bottle0,empty_bottle;6 intbottle=0;//prevent non-normal output7 8printf"Please enter the amount of money : \ n");9scanf"%d",&Money );Ten if(money<2) Oneprintf"your money will not buy goods!\n"); A Else -bottle=money/2; -bottle_top=money/2; theempty_bottle=money/2; - while((empty_bottle>=2)|| (bottle_top>=4))//change "wine bottle", then "wine cap" - { - +bottle=bottle+empty_bottle/2;//Beer bottles for beer caps, beer bottles-"beers-" beer caps + beer bottles, the maximum number of beer can be exchanged -bottle_top=bottle_top+empty_bottle/2;//bottle of wine bottles to produce beer cover number plus the original number of beer cover +Empty_bottle=empty_bottle%2+empty_bottle/2;//Bottle number of bottles and the number of beer bottles that have been left. A atbottle=bottle+bottle_top/4;//Beer cover for beer bottle, beer lid-beer-beer bottle + beer lid, can redeem maximum number of beer -empty_bottle=empty_bottle+bottle_top/4;//bottle of wine cover for the number of beer bottles plus the original number of beer bottles left -bottle_top=bottle_top%4+bottle_top/4;//bottles of wine cover converted to produce beer cover number plus the original remaining number of beer machine cover - - } -printf"You can drink%d bottles of beer!", bottle); inprintf"number of empty bottles left for redemption:%d!\n", empty_bottle); -printf"number of empty caps left for redemption:%d!\n", bottle_top); to}
Printing results:
Please enter the amount of money: 8 You can drink 11 bottles of beer ! The number of empty bottles left for redemption:1! number of empty caps for redemption: 3! Continue
Drinking beer.