【蓝桥杯】 [蓝桥杯 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;
}
相关推荐
顾安r2 小时前
11.8 脚本网页 星际逃生
c语言·前端·javascript·flask
报错小能手3 小时前
C++笔记——STL map
c++·笔记
LaoZhangGong1233 小时前
STM32 F103外部晶振8MHz改为12MHz,如何配置?
c语言·stm32·单片机·嵌入式硬件·晶振
思麟呀4 小时前
Linux的基础IO流
linux·运维·服务器·开发语言·c++
星释4 小时前
Rust 练习册 :Pythagorean Triplet与数学算法
开发语言·算法·rust
星释4 小时前
Rust 练习册 :Nth Prime与素数算法
开发语言·算法·rust
多喝开水少熬夜5 小时前
Trie树相关算法题java实现
java·开发语言·算法
QT 小鲜肉5 小时前
【QT/C++】Qt定时器QTimer类的实现方法详解(超详细)
开发语言·数据库·c++·笔记·qt·学习
WBluuue6 小时前
数据结构与算法:树上倍增与LCA
数据结构·c++·算法
bruk_spp6 小时前
牛客网华为在线编程题
算法