日常C语言题目代码

#define _CRT_SECURE_NO_WARNINGS
//方法一
#include <stdio.h>
int main()
{
	int n = 0;
	int total = 0;//总共能喝多少汽水
	int empty = 0;//表示手里的空瓶数
	scanf("%d",&n);
	//买n瓶汽水,且喝完后剩余n个空瓶
	total += n;
	empty += n;
	//重复用空瓶子买汽水直到空瓶子不够2个
	while (empty >= 2)
	{
		total += empty / 2;
		empty = empty / 2 + empty % 2;
	}
	printf("%d\n",total);
	return 0;
}


//方法2
#include <stdio.h>
int main()
{
	int n = 0;
	int total = 0;//总共能喝多少汽水
	int empty = 0;//表示手里的空瓶数
	scanf("%d",&n);
	//如果没钱买汽水,则喝到0瓶汽水,否则喝到2*n-1瓶汽水
	if (n == 0)
		total = 0;
	else
		total = 2 * n - 1;
	printf("%d\n", total);
	return 0;
}

题目描述:

已知汽水一块一瓶,两个空瓶子可以换一瓶汽水,输入整数n (n >= 0),表示n元钱,计算可以买多少瓶汽水,编程实现。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值