C语言:喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以喝多少汽水。编程实现。

题目:喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以喝多少汽水。使用编程实现。

代码实现如下:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int money = 0;
    int total = 0;
    int empty = 0;
    printf("请输入一个数:");
    scanf_s("%d", &money);
    total = money;
    empty = money;
    while (empty > 1)
    {
        total += empty / 2;
        empty = empty / 2 + empty % 2;
    }

    printf("可以喝 %d 瓶汽水\n", total);
    system("pause");
    return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_42270373/article/details/81280725