当前位置:网站首页>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,容易被绕进去.
边栏推荐
- 金九银十即将到来,求职套路多,面试指南我来分享~
- C语言:打印菱形
- 记录一次客户的APP数据库版本号升级失败的情况
- Laravel文档阅读笔记-Rendering JSON(对JS变量进行赋值)
- VOC格式标签转YOLO格式
- yolov5 detects the number of labels in the dataset
- Invoker 2019CCPC Qinhuangdao Station I Question Simple DP
- yolov5检测数据集标签数量
- rsync:recv_generator: mkdir (in backup) failed:Permission denied (13) |failed to set times on '.'
- 神经网络优化器
猜你喜欢
随机推荐
Sklearn data preprocessing
composer 内存不足够
数据库索引原理
C language: reverse character order
贪吃蛇小游戏——C语言
Record a failure to upgrade the client's APP database version number
String类创建的对象在JVM中的内存分配和equals与==的区别
更改Jupyter Notebook默认打开目录
74HC595芯片引脚说明
【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
tianqf的解题思路
SAP ALV 数据导出被截断的bug
pytorch指定GPU
list and string conversion
链表专项练习(三)
常用测试用例设计方法之正交实验法详解
定时任务组件Quartz
SSM整合开发案例
SAP ALV data export many of the bugs
重要消息丨.NET Core 3.1 将于今年12月13日结束支持