喝汽水--C语言

时间:2022-12-06 18:57:01

题目名称:

喝汽水问题

题目内容:

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

分析:

1.20元能喝20瓶

2.2个空瓶能换1瓶汽水,剩下的空瓶:empty/2+empty%

代码


方法1:

#include<stdio.h>
int main()
{
int money = 0;
int total = 0;
int empty = 0;
scanf("%d", &money);
total = money;
empty = money;
while (empty > 1)
{
total += empty / 2;
empty = empty / 2 + empty % 2;
}
printf("%d", total);
return 0;
}

方法2:(由方法1得等差数列(empty*2-1))

#include<stdio.h>
int main()
{
int money = 0;
int total = 0;
int empty = 0;
scanf("%d", &money);
total = money;
empty = money;
if (empty > 0)
total = empty * 2 - 1;
return 0;
}

感想:

没有撤退可言