首页 > 试题广场 >

汽水瓶

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

注意:本题存在多组输入。输入的 0 表示输入结束,并不用输出结果。

输入描述:

输入文件最多包含 10 组测试数据,每个数据占一行,仅包含一个正整数 n( 1<=n<=100 ),表示小张手上的空汽水瓶数。n=0 表示输入结束,你的程序不应当处理这一行。



输出描述:

对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。

示例1

输入

3
10
81
0

输出

1
5
40

说明

样例 1 解释:用三个空瓶换一瓶汽水,剩一个空瓶无法继续交换
样例 2 解释:用九个空瓶换三瓶汽水,剩四个空瓶再用三个空瓶换一瓶汽水,剩两个空瓶,向老板借一个空瓶再用三个空瓶换一瓶汽水喝完得一个空瓶还给老板    
本质上还是数学题,等价于 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)
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 n = in.nextInt();
            if (n == 0) {
                break;
            }
            System.out.println(count(n));
        }
    }

    public static int count(int n) {
        int num = 0;
        while (n > 2) {
            //消耗3个空水瓶
            n -= 3;
            //喝1瓶汽水
            num++;
            //得到1个空水瓶
            n++;
        }
        //若手中还有2个空水瓶,可借用一个空水瓶再喝一瓶
        if (n == 2) {
            num++;
        }
        return num;
    }
}
编辑于 2024-04-16 23:17:11 回复(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 re = mat(in.nextInt());
            if (re > 0) {
                System.out.println(re);
            }
        }
    }

    private static int mat(int v) {
        if (v < 2) {
            return 0;
        }
        if (v == 2) {
            return 1;
        }
        int sum = v / 3;
        sum += mat(v / 3 + v % 3);
        return sum;
    }
}


编辑于 2024-04-03 13:33:50 回复(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 a = in.nextInt();
            if (a == 0) return;
            int r = 0;
            while (a >= 3) {
                a -= 3;
                ++ a;
                ++ r;
            }
            if (a == 2) ++ r;
            System.out.println(r);
        }
    }
}

发表于 2024-03-23 22:24:09 回复(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){
                break;
            }
            int b = getResult(a);
            System.out.println(b);
        }
    }
    public static int getResult(int a){
        int result = 0;
        int last = a;
        while(last >3){
            int temp = last/3;
            int s = last % 3;
            last = temp + s ;
            result += temp;
             
        }
        if(last == 2 || last ==3){
            result++;
        }
        return result;
    }
}

编辑于 2024-03-14 22:24:53 回复(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) {
                continue;
            }
            int empty = n;
            int drink = 0;

            while (empty >= 2) {
                if (empty == 2) {
                    drink+=1;
                    empty = 1;
                    continue;
                }
                int mod = empty % 3;
                if (mod != 0) {
                    drink += (empty - mod) / 3;
                    empty = (empty - mod) / 3 + mod;
                } else {
                    drink += empty / 3;
                    empty = empty/3;
                }
                if(empty == 2) empty++;
            }
            System.out.println(drink);
        }

    }
}

编辑于 2024-03-06 23:30:39 回复(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 a = in.nextInt();
            if(a == 0) {
                break;
            }
            System.out.println(getJunkBeer(0,a));
        }
    }

    private static int getJunkBeer(int beerSize,int bSize) {
        int junkNum = 0;
        int n = bSize;
        if (n <= 1) {
            return beerSize;
        } else if(n == 2) {
            return beerSize +1;
        } else {
            int temple = n % 3;
            junkNum = n / 3;
            return getJunkBeer(beerSize + junkNum,junkNum+temple);
        }
    }
}
编辑于 2024-03-06 00:41:35 回复(0)
 
public class Demo001 {
    public static void main(String[] args){
        Scanner n = new Scanner(System.in);
        while(n.hasNextInt()){
            int a = n.nextInt();//输入的空汽水瓶数

            if(a%3+a/3>=2){
                int b = a/3+(a%3+a/3)/2;
                System.out.println(b);
            }else {
                int b = a / 3 + (a % 3 + a / 3) / 3;//计算出来的可以喝到的数量
                System.out.println(b);
            }

        }

    }


编辑于 2024-01-12 08:42:06 回复(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) {
                return;
            }
            int res = 0;
            int mark = 0;
            while (num > 1) {
                if(num ==2){
                    res ++;
                    num -= 2;
                }else{
                    res +=  num / 3;
                }
                mark = num % 3;
                num = mark + num / 3;
            }
            System.out.println(res);
        }
    }
}

编辑于 2023-12-06 14:06:35 回复(0)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int next = in.nextInt();
            if (next == 0) {
                return;
            }
            System.out.println(next >> 1);
        }
    }
}


发表于 2023-11-12 00:21:11 回复(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) {
                continue;
            }
            System.out.println(getBottle(a, 3));
        }
    }

    private static int getBottle(int total, int divisor) {
        // 差1的时候借1
        if (total == divisor - 1) {
            return 1;
        }
        if (total < divisor) {
            return 0;
        }

        int count =  total / divisor;
        return count + getBottle(count + total % divisor, divisor);
    }
}
发表于 2023-11-09 17:18:54 回复(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.hasNextLine()) { // 注意 while 处理多个 case
            int i=Integer.valueOf(in.nextLine());
            if(i!=0){
                System.out.println(i/2);
            }
        }
    }
}

发表于 2023-09-15 21:43:55 回复(0)
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int n = in.nextInt();
            if (n == 0) {
                break;
            }
            // 汽水数量
            int r = 0;
            while (n>=2){
                int a = n/3;
                r += a;
                int b = n%3;
                n = (a+b) == 2 ? 3:a+b;
            }
            System.out.println(r);
        }
    }
}
发表于 2023-09-14 21:37:08 回复(0)
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner in = newScanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while(in.hasNextInt()) { // 注意 while 处理多个 case
            inta = in.nextInt();
            if(a==0){
                break;
            }
            intb=0;//喝到汽水数量
            while(a>=2){
                b++;
                a++;
                a-=3;
            }
            System.out.println(b);
        }
    }
}
发表于 2023-09-06 15:09:21 回复(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 的区别
        int n = in.nextInt();
        while (n != 0){
            System.out.println(compute(n, 0));
            n = in.nextInt();
        }
    }
   
     // n:当前空瓶数,m:换到的汽水数
    public static int compute(int n, int m) {
        if (n == 2) {
            n++;
        }
        if (n / 3 > 0) {
            m += n / 3;
            n = n / 3 + n % 3;
            return compute(n, m);
        }
        return m;
    }
}
发表于 2023-08-16 19:00:36 回复(0)
import java.util.ArrayList;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<Integer> plans = new ArrayList<Integer>();
        while (true) {
            int bottles = Integer.parseInt(in.nextLine());
            if (bottles == 0)break;
            plans.add(bottles);
        }
        ArrayList<Integer> plansResult = new ArrayList<Integer>();
        for (int plane : plans) {
            int total = 0;
            while (plane > 2) {
                int remainde = plane % 3;
                int currentBottle = plane / 3;
                total += currentBottle;
                plane = currentBottle + remainde;
            }
            if (plane == 2) {
                total++;
            }

            plansResult.add(total);
        }
        plansResult.forEach(System.out::println);
    }
}

发表于 2023-08-09 04:07:36 回复(0)
简单题,循环即可解决
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
            int num = in.nextInt();
            if(num==0){
                break;
            }
            int count=0;  
            int shang=0;
            int yushuo=0;
            while(num>2){
                shang =num/3;
                yushuo=num%3;
                num=shang+yushuo;
                count+=shang;
            }
            if(num==2) count++;
            System.out.println(count);
        }
    }


发表于 2023-07-29 15:05:03 回复(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 n = in.nextInt();
            int count = 0;
            int zero = 0;
            while (n >= 3) {
                zero = n % 3;
                n /= 3;
                count += n;
                n += zero;
            }
            if (n >= 2) count++;
            if (n != 0)            System.out.println(count);
        }
    }
}
发表于 2023-07-15 15:27:50 回复(0)
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> empties = new ArrayList();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int emptyCount = in.nextInt();
            if(emptyCount == 0) {
                break;
            }

            empties.add(emptyCount);
        }
        empties.forEach(emptyCount-> {
            System.out.println(changeBottle(emptyCount, 0));
        });
    }

    public static int changeBottle(int emptyCount, int bottles) {
        if(emptyCount >= 3) {
            // 能够交换的汽水数
            int exchangedBottles = emptyCount / 3;
            // 剩余的空瓶数(给出3个换回一个相当于给出两个)
            emptyCount -= 2 * exchangedBottles;
            // 当前的汽水数
            bottles += exchangedBottles;
            return changeBottle(emptyCount, bottles);
        }
        // 两个空瓶可以借一个交换并归还
        if(emptyCount == 2) {
            return bottles + 1;
        }

        return bottles;
    }
}
发表于 2023-07-07 09:51:46 回复(0)

问题信息

难度:
232条回答 180425浏览

热门推荐

通过挑战的用户

查看代码