首页 > 试题广场 >

汽水瓶

[编程题]汽水瓶
  • 热度指数:535488 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。
\hspace{15pt}小张手上有 n 个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。

输入描述:
\hspace{15pt}本题将会给出 1 \leqq T \leqq 10 组测试数据,确切数字未知,您需要一直读入直到特定的结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每组测试数据描述如下:

\hspace{15pt}在一行上输入一个整数 n \left(0 \leqq n \leqq 100\right) ,代表小张手上的空汽水瓶数量。特别地,n=0 代表输入结束,您只需要立即退出,不需要针对这种情况进行处理。


输出描述:
\hspace{15pt}对于每一组测试数据,新起一行。输出一个整数,代表小张最多可以喝到的汽水数量。
示例1

输入

3
10
81
0

输出

1
5
40

说明

\hspace{15pt}对于第一组测试数据,共有 3 个空瓶,可以换 1 瓶汽水。可以证明无法再做任何兑换,因此最多可以喝到 1 瓶汽水。

\hspace{15pt}对于第二组测试数据:
\hspace{23pt}\bullet\,第一轮兑换,共有 10 个空瓶。可以换 3 瓶汽水,余下 1 个空瓶;
\hspace{23pt}\bullet\,第二轮兑换,刚刚余下 1 个空瓶、加上刚刚兑换的 3 瓶汽水喝完,共有 4 个空瓶。可以换 1 瓶汽水,余下 1 个空瓶;
\hspace{23pt}\bullet\,第三轮兑换,刚刚余下 1 个空瓶、加上刚刚兑换的 1 瓶汽水喝完、再找老板借 1 个空瓶,共有 3 个空瓶。可以换 1 瓶汽水,余下 0 个空瓶。喝完之后不要忘记归还借的空瓶。
\hspace{15pt}综上,一共可以喝到 3+1+1=5 瓶汽水。
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            int n = in.nextInt();
            if(n == 0) break;
            System.out.println(n / 2);
        }
        in.close();
    }
}

发表于 2025-03-24 19:43:20 回复(0)
我这个不是很优的方式。
模拟小张的行为,拆解为多轮,每一小张拿手上的空瓶去做兑换。每一能兑换的数量为
(当前手上的空瓶 - 当前手上的空瓶%3)/3  
这一轮兑换之后。小张手上剩下的瓶子数:(当前手上的空瓶 - 当前手上的空瓶%3)/3  + 当前手上的空瓶%3, 小张当前轮次喝到的瓶数为:(当前手上的空瓶 - 当前手上的空瓶%3)/3  
按照这个逻辑不停的进行兑换,直到手上的空瓶 小于3瓶,如果为2个空瓶,可以和老板借一个空瓶,再兑换一瓶。
import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    //方法一
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        List<Integer> list = new ArrayList<>();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int val = in.nextInt();
            int x = val;
            int num = 0;
            while (x >= 3) {
                int y = x % 3;
                int z = (x - y) / 3;
                num += z;
                x = z + y;
            }
            if (x == 2) {
                num += 1;
            }
            if (num > 0) System.out.println(num);

        }
    }
}

发表于 2025-03-21 10:27:55 回复(0)
其实就是2个瓶子等于一个汽水,总瓶子除以2就可以了    
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //遍历测试数据
        while (sc.hasNext()) {
            int num = sc.nextInt();
            if(num == 0){
                break;
            }
            System.out.println(num / 2);
        }
    }
发表于 2025-03-19 13:35:29 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            if (num != 0) {
                System.out.println((num / 2));
            }
        }
    }
}
发表于 2025-02-23 22:14:04 回复(0)
脑筋急转弯 
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) {
            int n = in.nextInt();
            if (n == 0) break;
            int cnt = 0;
            while (n >= 3) {
                cnt += n / 3;
                n = n - n / 3 * 3 + n / 3;
            }
            if (n == 2) cnt++;
            System.out.println(cnt);
        }
    }
}


发表于 2025-01-26 23:42:15 回复(0)

直接分成两种情况,持有瓶子数目 大于等2 或 小于2。

对于持有瓶子数目大于等于2的,每次只去换一瓶,持有瓶子数目-2(因为换来的喝完又算是一个空瓶子了)。
对于持有瓶子数目小于2的(0或1),一定喝不到。
用递归的方式不断对持有瓶子数和喝到的饮料数更新即可。
private static int canDrinkCount(int bottle) {
        if (bottle <= 1) {
            return 0;
        } else {
            return 1 + canDrinkCount(bottle - 2);
        }
    }

完整代码

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int bottle =  in.nextInt();
            if(bottle==0){
                return;
            }
            System.out.println(canDrinkCount(bottle));
        }
    }
    private static int canDrinkCount(int bottle) {
        if (bottle <= 1) {
            return 0;
        } else {
            return 1 + canDrinkCount(bottle - 2);
        }
    }
}


发表于 2024-11-17 03:11:57 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int a = in.nextInt();
            if(a == 0) return;
            int count = 0;
            while(a > 2){
                count += a/3;
                a = a/3 + a%3;
            }
            if(a == 2) count++;
            System.out.println(count);
        }
    }
}

发表于 2024-11-09 17:53:58 回复(1)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            if (a != 0) {
                System.out.println(a/2);
            }
        }
    }
}
发表于 2024-09-10 10:51:14 回复(0)
import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            if (n == 0) {
                break; // 输入结束标志
            }
            int result = drinkBottles(n);
            System.out.println(result);
        }

        scanner.close();
    }

    public static int drinkBottles(int bottles) {
        if(bottles == 2) return 1;
        
        if (bottles < 2) {
            // 如果瓶子数量不足3个,则不能再兑换汽水
            return 0;
        }

        // 用三个空瓶兑换一瓶汽水
        int drank = bottles / 3;

        // 剩余的瓶子数量
        int remaining = bottles % 3;

        // 如果剩下的瓶子加上兑换汽水后剩下的瓶子不足3个,则不能再兑换
        if (remaining + drank < 2) {
            return drank;
        }
        if (remaining + drank == 2) {
            return drank+1;
        }

        // 如果剩下的瓶子加上兑换汽水后剩下的瓶子足够3个,则可以再次兑换
        return drank + drinkBottles(remaining + drank);
    }
}
递归

发表于 2024-08-09 13:52:55 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (true) {
            int n = in.nextInt();
            if (n == 0) {
                break;
            }
            System.out.print((int)n / 2 + "\n");
        }
    }
}
发表于 2024-07-15 20:31:34 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            if (num == 0) {
                break;
            }
            int count = num / 2;
            System.out.println(count);
        }
    }
}

发表于 2024-07-06 17:40:07 回复(1)
一个简单的实现。虽然也许会有更好的方法?
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            if(n == 0) {break;}
            int count = n / 3;
            int left = count + (n - 3*count);
            while (left > 2) {
                count = count + (left / 3);
                left = (left / 3) + (left % 3);
            }
            if(left == 2) {
                count = count + 1;
                left = 0;
            }
            System.out.println(count);
        }
    }
}


发表于 2024-06-29 11:24:23 回复(0)
正经人不除2

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int number = in.nextInt();
            int exchange = getExchangeNumber(number);
            if (exchange <= 0) {
                break;
            }
            System.out.println(exchange);
        }
    }

    // 递归获取兑换瓶数
    public static int getExchangeNumber(int number) {
        if (number == 0) {
            return 0;
        }

        // 先拿到整除数, 兑换瓶数。
        int exchange = number / 3;
        // 剩余空瓶
        int last = number % 3;
        if (exchange <= 0 && last != 0) {
            // 兑换瓶数为0, 剩余为2时 可以借一瓶空瓶兑换
            return exchange + last < 2 ? 0 : 1;
        }
        
        // 兑换瓶数不为0,记录兑换瓶数
        exchange += getExchangeNumber(exchange + last);
        return exchange;
    }
}
发表于 2024-06-04 19:17:57 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int totalUseNum = 0;//总共喝了多少瓶
        int totalelseNum = 0;//手头总共剩多少瓶子
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            if (a == 0) {
            
            } else { 
                int IntNum = a / 3;//商
                totalUseNum = IntNum;
                int elseNum = a % 3;//余
                totalelseNum = elseNum+totalUseNum;
                for(int i=0;totalelseNum/3>=1;i++){
                    elseNum = totalelseNum % 3;
                    IntNum = totalelseNum / 3;                                   
                    totalUseNum = totalUseNum+IntNum;//所有的商加起来
                    totalelseNum = IntNum+elseNum;
                    //System.out.println(totalUseNum);
                    //System.out.println(totalelseNum);                  
                }//59 --29      94--47

                if(totalelseNum==2){//借1
                     totalUseNum = totalUseNum+1;
                }
                System.out.println(totalUseNum);

            }
                
        }
    }
}

发表于 2024-05-14 01:08:24 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int count = in.nextInt();
            if(count == 0) break;
            System.out.println(drink(count));


        }
    }
    public static int drink(int sum){
        int count = 0;
        int empty = 0;
        while((sum + empty) > 1){
            if((sum + empty) == 2){
                count++;
                sum = 0;
                empty = 0;
                break;
            }
            sum -= 3;
            empty++;
            count++;
        }
        return count;
    }
}
发表于 2024-05-01 12:05:47 回复(0)
本质上还是数学题,等价于 2个汽水瓶换1个汽水。但就算想不到,按题意硬解也没问题,题目里根本没埋坑。
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()) {
            int input = Integer.valueOf(in.nextLine());
            if(input == 0) {
                break;
            }

            int result = 0;
            while(input>=3) {
                int num = input/3;
                int mod = input%3;
                result += num;
                input = num+mod;
            }
            if(input==2) {
                result++;
            }

            System.out.println(result);
        }
    }
}


发表于 2024-04-29 10:21:02 回复(0)
思路:每剩下两个瓶可以借一个然后正好消耗完毕空瓶。
给定空瓶数分成n份2个空瓶一组,结果判断最后一组为2或者1情况,为2则恰好兑换n/2(空瓶消耗完毕),为1也恰好兑换n/2(余1无法兑换),所以最后结果都是n/2取整。
编辑于 2024-04-24 13:54:28 回复(0)