[关闭]
@yexiaoqi 2022-05-12T10:49:30.000000Z 字数 558 阅读 420

得分情况

刷题 华为机试


题目:一张100分试卷,判断10x2+单选10x4+多选5x8。从前往后做,错三个离场。给定一个分值,问有多少种情况


  1. public class Main {
  2. private static int sum = 0;
  3. 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};
  4. public static void main(String[] args){
  5. Scanner in = new Scanner(System.in);
  6. int target = in.nextInt();
  7. backtrack(0,0,0,target);
  8. System.out.println(sum);
  9. }
  10. private static void backtrack(int index, int score, int err, int target) {
  11. if (err>=3 || score>target) return;
  12. if (score == target) {
  13. sum++;
  14. return;
  15. }
  16. for (int i = index; i < scores.length; i++) {
  17. score += scores[i];
  18. backtrack(i+1, score, err, target);
  19. score -= scores[i];
  20. err++;
  21. }
  22. }
  23. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注