其他分享
首页 > 其他分享> > 喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水

喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水

作者:互联网

第一种方法
#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("你有多少钱:");
scanf_s("%d", &money);
int ret = Qishui(money);
printf("可以买%d瓶汽水\n", ret);
system("pause");
return 0;
}

第二种方法
#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("你有多少钱:");
scanf_s("%d", &money);
int ret = Qishui(money);
printf("可以买%d瓶汽水\n", ret);
system("pause");
return 0;
}

标签:汽水,20,int,money,Qishui,空瓶,total,empty
来源: https://blog.51cto.com/14947463/2547361