#define _CRT_SECURE_NO_WARNINGS
//方法一
#include <stdio.h>
int main()
{
int n = 0;
int total = 0;//总共能喝多少汽水
int empty = 0;//表示手里的空瓶数
scanf("%d",&n);
//买n瓶汽水,且喝完后剩余n个空瓶
total += n;
empty += n;
//重复用空瓶子买汽水直到空瓶子不够2个
while (empty >= 2)
{
total += empty / 2;
empty = empty / 2 + empty % 2;
}
printf("%d\n",total);
return 0;
}
//方法2
#include <stdio.h>
int main()
{
int n = 0;
int total = 0;//总共能喝多少汽水
int empty = 0;//表示手里的空瓶数
scanf("%d",&n);
//如果没钱买汽水,则喝到0瓶汽水,否则喝到2*n-1瓶汽水
if (n == 0)
total = 0;
else
total = 2 * n - 1;
printf("%d\n", total);
return 0;
}
题目描述:
已知汽水一块一瓶,两个空瓶子可以换一瓶汽水,输入整数n (n >= 0),表示n元钱,计算可以买多少瓶汽水,编程实现。
本文介绍了两种C语言实现方式,分别通过循环和条件判断解决了一个问题:给定一定金额,计算在允许用空瓶换取汽水的情况下,最多可以喝到多少瓶汽水。
176





