/************************************************************************************
2.每瓶汽水1元,两个空瓶可以置换一瓶汽水,现在有20元,
最多能喝多少瓶汽水。(编程实现)。
分析: 你n个瓶子换一个 就是 n-1块钱可以喝 n 瓶。(假设可以先借一瓶)
最简单办法 就是用公式: money/(n-1)*n+money%(n-1) money是你有多少钱
n 为多少个瓶子换一个 新汽水;
下面是 一般逻辑的代码:
************************************************************************************/
#include<stdio.h>
int count_bottle_num(int money)
{
int num = 0;
int null_bottle = 0;
int full_bottle = money;
while (1)
{
num += full_bottle;
null_bottle = full_bottle+null_bottle%2;
full_bottle = null_bottle / 2;
if (null_bottle == 1)
{
return num+1;
}
}
}
int main()
{
int money = 0;
printf("请输入金额:");
scanf("%d",&money);
printf("%d元钱可以喝%d瓶汽水!!\n",money,count_bottle_num(money));
return 0;
}
转载于:https://blog.51cto.com/shaungqiran/1683110