当前位置:网站首页>喝汽水问题
喝汽水问题
2022-04-21 15:48:00 【zhanghangqian】
1.某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。
小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。
数据范围:输入的正整数满足 1 \le n \le 100 \1≤n≤100
注意:本题存在多组输入。输入的 0 表示输入结束,并不用输出结果。
输入描述:
输入文件最多包含 10 组测试数据,每个数据占一行,仅包含一个正整数 n( 1<=n<=100 ),表示小张手上的空汽水瓶数。n=0 表示输入结束,你的程序不应当处理这一行。
输出描述:
对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。
示例1
输入:
3 10 81 0
输出:
1 5 40
说明:
样例 1 解释:用三个空瓶换一瓶汽水,剩一个空瓶无法继续交换 样例 2 解释:用九个空瓶换三瓶汽水,剩四个空瓶再用三个空瓶换一瓶汽水,剩两个空瓶,向老板借一个空瓶再用三个空瓶换一瓶汽水喝完得一个空瓶还给老板
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sca = new Scanner(System.in);
while(sca.hasNext()){
int n = sca.nextInt();
if(n > 1){
System.out.println(func(n));
}
}
}
public static int func(int num){
int sum = 0;
while(num > 1){
//第一次换的汽水个数
sum += num /3;
num = num / 3 + num % 3;
if(num == 2){
sum++;
break;
}
}
return sum;
}
}
版权声明
本文为[zhanghangqian]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zhanghangqian/article/details/124286195
边栏推荐
- How can I easily manage harbor in multi-user scenarios!
- Loading and unloading of embedded driver module
- Application of food safety law and relevant laws in patent examination
- ABAP keywords at first and at last learning
- 一文读懂PlatoFarm新经济模型以及生态进展
- Easy language CeF3 get the source code returned by the request
- sed命令
- AcWing 1866. Fence painting (section crossing)
- 【unity笔记】L2Unity Shader基础
- EL&JSTL
猜你喜欢

Sed command

Avril 2022 classement de la base de données de la Chine: la brise printanière se réchauffe et les scores rebondissent en avril

ABAQUS loads according to coordinates - analytical field load mapping

Applet introduction and development tools

ABAP keywords at first and at last learning

LeetCode 733、图像渲染

AcWing 1788. 牛为什么过马路(模拟)

解决I.MX6U驱动移植LED闪烁

LeetCode 21、合并两个有序链表

「查缺补漏」,DDD 核心概念梳理
随机推荐
Embedded development: three skills of reusing development board for testing
6 IMPORTANT JOB SKILLS YOU NEED AS A NETWORK CABLE TECHNICIAN
China Database ranking in April 2022: the spring breeze blows the face, the spring is warm, and the score rises in April
With Zhongfu Jinshi's stock speculation, the industry has precipitated for 25 years, and the investment should speak with strength
做程序员为什么这么累?
Ji Geng 44 / 90
许远东受邀上海管理科技论坛做《LTD数字化经营方法论》分享
LeetCode-232-用栈实现队列
C语言进阶第42式:内存操作经典问题分析二
Ji Geng 56 / 90
「查缺补漏」,DDD 核心概念梳理
解决I.MX6U驱动移植LED闪烁
季更45/90
Easy Language cef3 get request returned source
Programmer meituan interview experience, from basic to algorithm, lasted six hours, with an annual salary of 20W
Web. Detailed explanation of XML file
嵌入式驱动模块的加载与卸载
LeetCode 1539、第 k 个缺失的正整数
How can I easily manage harbor in multi-user scenarios!
"Checking and remedying deficiencies", sorting out the core concepts of DDD