Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

  • 字符串

  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

    • 数组
    • 二分
    • 树
    • 动态规划
    • 回溯
      • 考试得分情况[华为]
    • 图
  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 笔试真题
Nreal
2024-01-08
目录

回溯

# 考试得分情况[华为]

题目描述:

25道题,10道2分,10道4分,5道8分,共100分
累计有3题错误,终止考试,计算得分
输入N:结果分数
输出:所有答题情况个数
案例:
94 :得分
100:100种情况,1道判断1道单选错误,其余都对
1
2
3
4
5
6
7
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.nextLine();
        scores = new int[N];
        for(int i=0;i<10;i++){
            scores[i] = 2;
        }
        for(int i=10;i<20;i++){
            scores[i] = 4;
        }
        for(int i=20;i<25;i++){
            scores[i] = 8;
        }
        System.out.println(dfs(0,N,3));
    }
    static int[] scores;
    static HashMap<String,Integer> memo = new HashMap<>();

    /**
     * @param i 题目索引
     * @param rest 剩余分数
     * @param cnt 做错题数
     * @return
     */
    static int dfs(int i,int rest,int cnt){
        if(i>=scores.length){
            return 0;
        }
        if(cnt == 0) return rest==0?1:0;
        if(rest == 0) return 1;
        String stat = i+"-"+rest+"-"+cnt;
        if(memo.containsKey(stat)){
            return memo.get(stat);
        }
        int ans = dfs(i+1,rest,cnt-1)+dfs(i+1,rest-scores[i],cnt);
        memo.put(stat,ans);
        return ans;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
动态规划
图

← 动态规划 图→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式