新员工考试,共25题,前10题每题2分,后10题每题4分,再后面每题8分,最多能错3题,错三题后提前交卷,问达到目标分数情况个数。
public static int getCount(int target){
return ecrurrence(0, true, 0, target,0) + ecrurrence(0,false,0,target,0);
}
public static int ecrurrence(int index, boolean isTrue, int points, int target,int sum) {
//判断当前位置是不是错误的
if (isTrue) {
if (index < 10) {
points += 2;
} else if (index < 20) {
points += 4;
} else {
points += 8;
}
}else {
sum ++;
}
//如果当前错了三个
if (sum == 3) {
if (points == target) {
// i++;
return 1;
}
return 0;
}
// 判断当前分数是否满足条件
if (points == target) {
return 1;
}
// 如果当前分数不满足条件并且是最后一个数
if (index == 24) {
return 0;
}
//如果当前分数比目标分数低
if (points < target) {
index++;
return ecrurrence(index, false, points, target,sum) + ecrurrence(index, true, points, target,sum);
}
// 其他情况
return 0;
}