7

喝饮料问题,饮料价格1元/瓶,两瓶饮料空瓶可以重新兑换一瓶饮料,问20元可以喝多少瓶...

 3 years ago
source link: https://segmentfault.com/a/1190000040426248
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

喝饮料问题,饮料价格1元/瓶,两瓶饮料空瓶可以重新兑换一瓶饮料,问20元可以喝多少瓶饮料

发布于 7 分钟前
int main()//方法一
{
    int money = 0;//手上的钱,即用钱买回来的汽水数量
    int total = 0;//买的+兑的
    int empty = 0;//手里的空瓶
    scanf_s("%d", &money);
    int sum = totalsum(money);
    printf("sum=%d\n", sum);
    total = money;//用钱买回来的汽水数量
    empty = money;//喝完用钱买的汽水后手里的空瓶数量
    while (empty>=2)//只要空瓶不低于两瓶就可以换汽水
    {
        total += empty / 2;
        empty = empty / 2 + empty % 2;//空瓶的数量为换回来的空瓶+手里余下的空瓶
    }
    printf("total=%d\n", total);
}
方法2,因为两个空瓶换一瓶饮料,一瓶饮料1块钱,相当于1个空瓶等于0.5元,20块钱可以换40个空瓶,相当于喝了40瓶汽水,但因为最后手里留了一个空瓶,所以只能换来39个空瓶,即喝了39瓶汽水
int totalsum(int money)
{
    int sum = 0;
    if (money== 0)//手里没钱就不能喝饮料
    {
        return 0;
    }
    else
    {
        sum = money * 2 - 1;
        return sum;
    }
    
//}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK