3

喝汽水--C语言_wx61f0a33ec5047的技术博客_51CTO博客

 1 year ago
source link: https://blog.51cto.com/u_15501985/5916391
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

题目名称:

喝汽水问题

题目内容:

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

1.20元能喝20瓶

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

#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;
}

没有撤退可言


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK