【蓝桥杯】 [蓝桥杯 2015 省 A] 饮料换购

原题链接:https://www.luogu.com.cn/problem/P8627

1. 题目描述

2. 思路分析

小伙伴们如果没有思路可以看看这篇文章~(这里很详细讲解了三种方法!)

https://blog.csdn.net/m0_62531913/article/details/132385341?spm=1001.2014.3001.5501

我们这里主要讲下方法二的推导过程:

列方程。

设最后喝了x瓶饮料,则共有x-n瓶饮料是换购来的。因为最后1个瓶盖无法换购,那么实际参与了换购的瓶盖只有x-1个。

则可以列出方程:x-n=(x-1)/3

最后解得:x=(3*n-1)/2;

故答案为:(3*n-1)/2

3. 代码实现

3.1 方法一

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int empty = n;
	int total = n;
	while (empty>=3)
	{
		total += empty / 3;
		empty = empty / 3 + empty % 3;
	}
	cout << total << endl;
	return 0;
}

3.2 方法二

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	cout<<(3*n-1)/2;
	return 0;
}

3.3 方法三

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int empty = 0;
	while (n)
	{
		n--;
		empty++;
		if (empty % 3 == 0)
			empty++;
	}
	cout << empty << endl;
}
相关推荐
Navigator_Z9 小时前
LeetCode //C - 1304. Find N Unique Integers Sum up to Zero
c语言·算法·leetcode
Yyyyyy~9 小时前
【C++】String
c++
晓蛋9 小时前
C语言第1章概述.ppt
c语言·程序设计·概述·关键字·编译环境
会编程的小孩17 小时前
C++ qt的安装
c++
学逆向的18 小时前
C 枚举变量
c语言·开发语言·网络安全
冷凝雨18 小时前
前导零、后导零、最高有效位、最低有效位计算(C)
c语言
鸥梨菌Honevid18 小时前
C 语言 restrict 关键字深度解析:从“独占访问”承诺到编译器激进优化
c语言
企业数字化笔记20 小时前
视频人体姿态怎么做三维可视化?2D关键点、深度局限与技术选型
人工智能·算法·音视频
?abc!20 小时前
(数组)581. 最短无序连续子数组
数据结构·算法·leetcode