1. 程式人生 > 其它 >牛客華為機試HJ22

牛客華為機試HJ22

原題傳送門

1. 問題描述

2. Solution

Java

package huawei.HJ022;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;

public class Main {
    static Scanner in;
    static String inputFileName = "/Users/jun/Documents/Learn/JavaLearning/NowCoder/src/huawei/HJ020/input.txt";

    static {
        if (!"Linux".equals(System.getProperty("os.name"))) {
            try {
                in = new Scanner(Paths.get(inputFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            in = new Scanner(System.in);
        }
    }

    public static void main(String[] args) {
        while (in.hasNext()) {
            int n = in.nextInt();
            if (n != 0)
                solve(n);
        }
    }

    static void solve(int n) {
        int res = 0;
        while (n > 3) {
            res += n / 3;
            n = n / 3 + n % 3;
        }
        // 1 -> 0, 2 -> 1, 3 -> 1
        if (n != 1)
            res++;
        System.out.println(res);
    }
}

Python

import sys

if sys.platform != "linux":
    sys.stdin = open("input/HJ22.txt")


def solve(n):
    res = 0
    while n > 3:
        res += n // 3
        n = n // 3 + n % 3
    # 1 -> 0, 2 -> 1, 3 -> 1
    if n != 1:
        res += 1
    print(res)


for line in sys.stdin:
    n = int(line.strip())
    if n != 0:
        solve(n)