当前位置: 首页 > news >正文

【蓝桥杯】 [蓝桥杯 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 方法一

#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 方法二

#include<bits/stdc++.h>
using namespace std;int main()
{int n;cin>>n;cout<<(3*n-1)/2;return 0;
}

3.3 方法三

#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;
}

http://www.lryc.cn/news/139219.html

相关文章:

  • 操作系统-笔记-第三章-内存管理
  • 详解单体架构和微服务(概念,优缺点和区别)
  • 储能运行约束的Matlab建模方法
  • 微信小程序 车牌号输入组件
  • Bootstrap Blazor 实战动态表单组件
  • Elasticsearch 集成---Spark Streaming 框架集成
  • Kotlin 中的 协程 基础篇
  • SQL事务
  • 关于flutter中 initState() 与 setState() 用法
  • 智能电话机器人是如何自主学习的
  • 【Rust】Rust学习 第十八章模式用来匹配值的结构
  • 我的学习笔记:数据处理
  • GB28181国标平台测试软件NTV-GBC(包含服务器和模拟客户端)
  • 云原生:重塑企业的技术疆界
  • 华为星闪,一项将 “ 更稳 WiFi ” 和 “ 更好蓝牙 ” 融合起来的通信标准
  • IDEA创建Mybatis格式XML文件
  • 二叉树中的最大路径和-递归
  • Python if-else 速记
  • Python使用内置的json模块来处理JSON数据
  • 亿赛通电子文档安全管理系统 RCE漏洞
  • 信息安全面试题合集
  • vue 简单实验 自定义组件 传参数 props
  • 目标检测笔记(十一):如何结合特定区域进行目标检测(基于OpenCV的人脸检测实例)
  • PID直观感受简述
  • Tomcat运行后localhost:8080访问自己编写的网页
  • 传感网应用开发1+X实训室建方案
  • PDF校对:让您的文件无瑕疵
  • SpringBoot--解决空字符串转枚举异常
  • Redis的常用数据类型详解
  • jpa里IdentityGenerator和IncrementGenerator的区别