喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,
给20元,可以多少汽水。
编程实现。
int Mount(int money)
{
int num = money;
int bottle = num; //20块钱可以拥有20个空瓶
while (bottle >= 2)
{
num += bottle / 2; //20瓶可以换的汽水
if (bottle % 2 == 0)
{
bottle /= 2;
}
else
{
bottle = bottle / 2 + 1;
}
}
return num;
}
int main()
{
int num = Mount(20);
printf("共能买到%d瓶汽水\n", num);
system("pause");
return 0;
}
运行结果: