@yexiaoqi
2022-05-12T10:49:30.000000Z
字数 558
阅读 420
刷题
华为机试
题目:一张100分试卷,判断10x2+单选10x4+多选5x8。从前往后做,错三个离场。给定一个分值,问有多少种情况
public class Main {
private static int sum = 0;
public static int[] scores = {2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,8,8,8,8,8};
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int target = in.nextInt();
backtrack(0,0,0,target);
System.out.println(sum);
}
private static void backtrack(int index, int score, int err, int target) {
if (err>=3 || score>target) return;
if (score == target) {
sum++;
return;
}
for (int i = index; i < scores.length; i++) {
score += scores[i];
backtrack(i+1, score, err, target);
score -= scores[i];
err++;
}
}
}