当前位置:网站首页>Drink soda, a bottle of soda water 1 yuan, two empty bottles can change a bottle of soda, give 20 yuan, how much soda can you

Drink soda, a bottle of soda water 1 yuan, two empty bottles can change a bottle of soda, give 20 yuan, how much soda can you

2020-11-08 12:56:00 open_51airx3z

The first method
#include<stdio.h>
#include<Windows.h>
#pragma warning(disable:4996)
int Qishui(int n)
{
int total = 0;
total += n;
for (; n >= 2;n=n/2+n%2)
{
total += n / 2;
}
return total;
}
int main()
{
int money = 0;
printf(" How much money do you have? :");
scanf_s("%d", &money);
int ret = Qishui(money);
printf(" Can buy %d Bottle of soda \n", ret);
system("pause");
return 0;
}






















The second method
#include<stdio.h>
#include<Windows.h>
#pragma warning(disable:4996)
int Qishui(int money)
{
int total = money;
int empty = money;
while (empty > 1)
{
total += empty / 2;
empty = empty / 2 + empty % 2;
}
return total;
}
int main()
{
int money = 0;
printf(" How much money do you have? :");
scanf_s("%d", &money);
int ret = Qishui(money);
printf(" Can buy %d Bottle of soda \n", ret);
system("pause");
return 0;
}























版权声明
本文为[open_51airx3z]所创,转载请带上原文链接,感谢