【蓝桥杯】 [蓝桥杯 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;
}
相关推荐
一只齐刘海的猫2 分钟前
【Leetcode】三数之和
数据结构·算法·leetcode
lightqjx2 分钟前
【算法】数据结构_扩展域并查集
数据结构·算法·并查集·扩展域并查集
caimouse3 分钟前
Reactos 第 3 章 内存管理 — 【下篇】换出、Section、池
c语言·开发语言·windows·架构
San813_LDD5 分钟前
[量化]《多线程数据同步精讲:std::mutex 的底层原理与最佳实践》
c语言·数据结构
无忧.芙桃6 分钟前
debug实例与分析(一)
开发语言·c++·算法
alwaysrun9 分钟前
C++之类型安全格式化format
c++·程序员·编程语言
sheeta199812 分钟前
LeetCode 补拙笔记 日期:2026.06.07 题目:49. 字母异位词分组
笔记·算法·leetcode
邪修king12 分钟前
C++ 哈希表超全详解:从底层实现到封装 myunordered_map/myunordered_set
c++·哈希算法·散列表
secret_to_me15 分钟前
buildRoot编译rootfs实战
linux·c语言·c++·ubuntu·电脑·buildroot
凡人叶枫16 分钟前
Effective C++ 条款01:视 C++ 为一个语言联邦
linux·开发语言·c++·effective c++·编程范式·语言联邦