【蓝桥杯】 [蓝桥杯 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;
}
相关推荐
MicroTech20251 天前
微算法科技(MLGO)研发突破性低复杂度CFG算法,成功缓解边缘分裂学习中的掉队者问题
科技·学习·算法
墨染点香1 天前
LeetCode 刷题【126. 单词接龙 II】
算法·leetcode·职场和发展
aloha_7891 天前
力扣hot100做题整理91-100
数据结构·算法·leetcode
Tiny番茄1 天前
31.下一个排列
数据结构·python·算法·leetcode
挂科是不可能出现的1 天前
最长连续序列
数据结构·c++·算法
前端小L1 天前
动态规划的“数学之魂”:从DP推演到质因数分解——巧解「只有两个键的键盘」
算法·动态规划
RTC老炮1 天前
webrtc弱网-ReceiveSideCongestionController类源码分析及算法原理
网络·算法·webrtc
一念&1 天前
每日一个C语言知识:C 结构体
c语言·开发语言
mjhcsp1 天前
C++ int 类型深度解析:从底层实现到实战应用
c++·int
21号 11 天前
9.Redis 集群(重在理解)
数据库·redis·算法