1. 程式人生 > >ccf 201612-02工資計算 java(100分)

ccf 201612-02工資計算 java(100分)

沒有使用分段函式的方法計算出稅前工資,

使用的是暴力破解,比較不費腦

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class TaxRate{
    public int min;
    public int max;
    public float rate;
    public float money;

    public TaxRate(int min, int max, float rate) {
        this.min = min;
        this.max = max;
        this.rate = rate;
        this.money = rate * (max - min);
    }
}

public class Main{
    private static List<TaxRate> taxRates = new ArrayList<>();

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int getMoney = Integer.parseInt(scanner.nextLine());
        scanner.close();

        taxRates.add(new TaxRate(0, 1500, 0.03f));
        taxRates.add(new TaxRate(1500, 4500, 0.1f));
        taxRates.add(new TaxRate(4500, 9000, 0.2f));
        taxRates.add(new TaxRate(9000, 35000, 0.25f));
        taxRates.add(new TaxRate(35000, 55000, 0.3f));
        taxRates.add(new TaxRate(55000, 80000, 0.35f));
        taxRates.add(new TaxRate(80000, Integer.MAX_VALUE, 0.45f));

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

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