当前位置:网站首页>C language: detailed explanation of soda bottle
C language: detailed explanation of soda bottle
2022-08-09 07:45:00 【Gaoyou Wu Shao】
题目链接:汽水瓶


#include<stdio.h>
int main()
{
//多组输入
int n = 0;
while ((scanf("%d", &n) != EOF))
{
if (n == 0)
{
return 0;
}
else {
int count = 0;//计数器
int dh = 0;//Redeem for soda
while (((n + 1) / 3) != 0)//(n+1)/3!=0Instructions can also be exchanged
{
if (n != 2)//Not borrowed1bottle change
{
dh = n / 3;
//If the number of empty bottlesn正好是3的倍数,No need to borrow it
//If the bottle is emptyn差1个是3的倍数,可以借一个
n = dh + n % 3;//Leftover soda before redemption+Redeem for soda=The number of new empty bottles
count += dh;
dh = 0;
}
else//最后剩下2个空瓶,Borrow one for another,Finally returned the empty bottle
{
count++;
break;
}
}
printf("%d\n", count);
}
}
return 0;
}
There is a misunderstanding in this question,就是“借一瓶”Borrowing a bottle of this is not always available.
比如你现在29空瓶,Although the title of it is that you can borrow an empty bottle,好像就30An empty bottle,Then you redeem at once10个.
其实不是这样的.It is required that you use it first29个空瓶换9个,然后剩余2+9个空瓶,And then exchange to get3瓶.
3The bottle is finished,剩余2+3个空瓶,And then exchange to get1瓶.
1The bottle is finished,剩余2+1个空瓶,And then exchange to get1瓶.
1Borrow the bottle again1The bottle is also not enough to redeem,循环结束.示意图如下:
This borrowing a bottle for redemption happens at the end of your leftovers2The case of an empty bottle,
你借1Get an empty bottle3空瓶,Then just change1个,Return the empty bottle after drinking.
其他情况,Borrowing is not allowed,This is also where this topic is more pitted,容易被绕进去.
边栏推荐
- 重要消息丨.NET Core 3.1 将于今年12月13日结束支持
- 【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- 记录一次客户的APP数据库版本号升级失败的情况
- 2019 Nanchang Internet Competition Question C, Hello 2019
- SDRAM的数据存储实现并对其数据进行读写操作
- 74HC595 chip pin description
- 力扣208,实现Trie(前缀树)
- 我的创作纪念日
- P1505 [National Training Team] Tourism Tree Chain Breakdown
- Invoker 2019CCPC秦皇岛站I题 简单DP
猜你喜欢
随机推荐
常用测试用例设计方法之正交实验法详解
学习小笔记---机器学习
2017icpc沈阳 G Infinite Fraction Path BFS+剪枝
一键登陆服务器脚本
Invoker 2019CCPC Qinhuangdao Station I Question Simple DP
74HC595芯片引脚说明
(三)、时间序列预测
MUV LUV EXTRA 2019CCPC秦皇岛站J题 KMP
【模板】树链剖分 P3384
MUI无法滚动?完美解决
Data storage implementation of SDRAM and read and write operations on its data
Anaconda replaces the default virtual environment
postgresql窗口功能
贪吃蛇小游戏——C语言
Anaconda 更换默认虚拟环境
Native JDBC operation database
生成对抗网络GAN:Generative Adversarial Networks
灵活好用的sql monitoring 脚本 part7
EXCEL使用函数联调(find,mid,vlookup,xlookup)
redis学习笔记









