博客
关于我
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/

你可能感兴趣的文章
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
Orleans框架------基于Actor模型生成分布式Id
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>