这道题可以通过题目的条件反推出计算工资的方程式。
我觉得如果我参加考试的话,在考场上应该没有办法冷静下来推算方程式诶。所以我使用暴力破解。
因为找到答案后就会跳出循环,这样也不会超时。
奉上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;
}
}