201612-2 工資計算

這道題可以通過題目的條件反推出計算工資的方程式。

我覺得如果我參加考試的話,在考場上應該沒有辦法冷靜下來推算方程式誒。所以我使用暴力破解。

因爲找到答案後就會跳出循環,這樣也不會超時。

奉上java滿分代碼

import java.util.*;

public class Main{
    static class Tax{
        public int max;
        public int min;
        public float rate;

        public Tax(int min, int max, float rate) {
            this.max = max;
            this.min = min;
            this.rate = rate;
        }
    }

    private static List<Tax> taxes = new ArrayList<>();
    public static void main(String[] args){
        taxes.add(new Tax(0, 1500, 0.03f));
        taxes.add(new Tax(1500, 4500, 0.1f));
        taxes.add(new Tax(4500, 9000, 0.2f));
        taxes.add(new Tax(9000, 35000, 0.25f));
        taxes.add(new Tax(35000, 55000, 0.3f));
        taxes.add(new Tax(55000, 80000, 0.35f));
        taxes.add(new Tax(80000, Integer.MAX_VALUE, 0.45f));

        Scanner scanner = new Scanner(System.in);
        int money = Integer.parseInt(scanner.nextLine());
        scanner.close();

        if(money <= 3500){
            System.out.println(money);
        } else {
            for(int i = 3500; i <= Integer.MAX_VALUE; i++){
                if(i - getTax(i) == money){
                    System.out.println(i);
                    break;
                }
            }
        }

    }

    private static float getTax(int money){
        if(money <= 3500)
            return money;
        money -= 3500;
        float sum = 0;
        for(Tax tax : taxes){
            if(money >= tax.max){
                sum += (tax.max - tax.min) * tax.rate;
            } else{
                sum += (money - tax.min) * tax.rate;
                break;
            }
        }
        return sum;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章