博客
关于我
1255: [蓝桥杯2015初赛]饮料换购 【水题】
阅读量:123 次
发布时间:2019-02-26

本文共 515 字,大约阅读时间需要 1 分钟。

瓶盖兑换问题的计算方法

在这个问题中,我们需要计算喝瓶子的数量,并根据瓶盖数量兑换新的瓶子。具体来说,喝三瓶可以兑换一瓶,这相当于每次喝两瓶实际上可以得到一瓶补偿。因此,我们可以通过以下方法来计算总共需要喝多少瓶子。

假设初始有n瓶子,我们需要通过循环的方式计算总共需要喝的瓶数。每次喝两瓶,得到一瓶补偿,这样总瓶数会逐渐减少。具体步骤如下:

int n; while (cin >> n) { int sum = n; int temp = n; while (temp >= 3) { sum += temp / 3; temp = temp / 3 + temp % 3; } cout << sum << endl; }

优化思路:喝两瓶换一瓶

在这个优化方案中,我们意识到喝两瓶可以兑换一瓶,这样总瓶数会减少。具体来说,每次喝两瓶,相当于净喝一瓶。因此,我们可以通过以下方式来计算总共需要喝的瓶数:

int n; while (cin >> n) { int ans = 0; while (n >= 3) { n = n - 2; ans = ans + 3; } cout << ans + n << endl; }

转载地址:http://hrfy.baihongyu.com/

你可能感兴趣的文章
NPM使用前设置和升级
查看>>
npm入门,这篇就够了
查看>>
npm切换到淘宝源
查看>>
npm前端包管理工具简介---npm工作笔记001
查看>>
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和yarn清理缓存命令
查看>>
npm和yarn的使用对比
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>
NPOI在Excel中插入图片
查看>>
NPOI格式设置
查看>>
Npp删除选中行的Macro录制方式
查看>>