题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
方法一:模拟
import java.io.*;
import java.util.*;
public class Main {
    public static void  main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        String str = null;
        while ((str = br.readLine()) != null) {
            int n = Integer.parseInt(str);
            if (n == 0) {
                break;
            }
            pw.println(sodaBottle(n));
        }
        pw.flush();
        pw.close();
        br.close();
    }
    private static int sodaBottle(int n) {
        int ans = 0;
        while (n > 1) {
            ans += n / 3;
            n = n / 3 + n % 3;
            if (n == 2) {
                n = 3;
            }
        }
        return ans;
    }
}
方法二:数学
某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。
如果有两个瓶子,则可以借一个瓶子,这样就有 3 个瓶子,可以换一瓶汽水,喝完后还一个瓶子。
等价于 两个瓶子换一瓶汽水。
即答案为 n / 2。
 投递福建联通等公司10个岗位
投递福建联通等公司10个岗位