当前位置:网站首页>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,容易被绕进去.
边栏推荐
猜你喜欢
随机推荐
One-click login server script
一键登陆服务器脚本
Codeforces Round #359 (Div. 2) C. Robbers' watch Violent Enumeration
定时任务组件Quartz
ImportError: cannot import name ‘imresize‘
SiamFC:用于目标跟踪的全卷积孪生网络 fully-convolutional siamese networks for object tracking
MUV LUV EXTRA 2019CCPC Qinhuangdao Station J Question KMP
String类创建的对象在JVM中的内存分配和equals与==的区别
日期处理,字符串日期格式转换
Tkinter可以选择的颜色
composer 内存不足够
Colors that Tkinter can choose from
nvm安装以及管理多版本node教程
c语言位段
yolov5 detects the number of labels in the dataset
Use tensorflow.keras to build a neural network model modularly
我的创作纪念日
2019 Nanchang Internet Competition Question C, Hello 2019
SSL证书最长有效期13个月,还有必要一次申请多年吗?
查看日志常用命令