当前位置:网站首页>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,容易被绕进去.
边栏推荐
猜你喜欢
Pytorch中 nn.BatchNorm2d() 归一化操作
差分约束-图论
种子数据报错:liquibase.exception.ValidationFailedException: Validation Failed
Important news丨.NET Core 3.1 will end support on December 13 this year
unity第一课
毕业我选择了保家卫国,退伍我选择了华为外包
Tkinter可以选择的颜色
postgresql Window Functions
jmeter并发数量以及压力机的一些限制
【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
随机推荐
Colors that Tkinter can choose from
Classes and Structures
Pytorch 训练技巧
Difference Constraint - Graph Theory
Sklearn data preprocessing
PyTorch中 torch.nn与torch.nn.functional的区别
c语言位段
2019南昌网络赛 C题,Hello 2019
日期处理,字符串日期格式转换
HDU - 3183 A Magic Lamp 线段树
常用测试用例设计方法之正交实验法详解
Lottie系列三 :原理分析
排序第三节——交换排序(冒泡排序+快速排序+快排的优化)(5个视频讲解)
SAP ALV data export many of the bugs
C语言:字符逆序
nvm安装以及管理多版本node教程
A brief introduction to microservice architecture
Apache POI
环形链表问题(判环、求入口点)
神经网络优化器