Zhzzh12580 2024-09-10 23:16 采纳率: 0%
浏览 33
已结题

java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。

难度数量之和等于各类型试题数量之和,已知题库中各种类型的题的数量,求按照给定的难度数量和各试题数量能否随机抽题,如果能的话,每一种题抽几道。(不能使用3层以上for循环,保证效率,抽题数量可能为100道)

img

  • 写回答

30条回答 默认 最新

  • CSDN专家-sinJack 2024-09-11 09:42
    关注
    获得1.00元问题酬金
    import java.util.Arrays;
    public class TestRandom {
        public static void main(String[] args) {
            int[] types = {4, 3, 2};// 简单、普通、困难题的数量
            int[] ratios = {5, 3, 2};// 单选、多选、判断题的数量
            int[][] questionCounts = {
                    {1, 1, 1}, // 简单题
                    {2, 2, 1},  // 普通题
                    {2, 1, 0}   // 困难题
            };
    
            int[][] selectedQuestions = selectQuestions(ratios, types, questionCounts);
            System.out.println(Arrays.deepToString(selectedQuestions));
        }
    
        public static int[][] selectQuestions(int[] ratios, int[] types, int[][] questionCounts) {
            int numDifficulties = ratios.length;
            int numTypes = types.length;
    
            // 计算总题数
            int totalQuestions = 0;
            for (int count : ratios) {
                totalQuestions += count;
            }
            for (int count : types) {
                totalQuestions += count;
            }
    
            // 初始化结果数组
            int[][] selectedQuestions = new int[numDifficulties][numTypes];
    
            // 分配题目数量
            for (int d = 0; d < numDifficulties; d++) {
                int remainingQuestions = ratios[d];
                for (int t = 0; t < numTypes; t++) {
                    int maxPossible = Math.min(remainingQuestions, questionCounts[d][t]);
                    selectedQuestions[d][t] = maxPossible;
                    remainingQuestions -= maxPossible;
                }
            }
    
            // 验证分配结果
            int totalSelected = 0;
            for (int[] row : selectedQuestions) {
                for (int count : row) {
                    totalSelected += count;
                }
            }
    
            if (totalSelected != totalQuestions) {
                throw new IllegalArgumentException("无法满足题目数量要求");
            }
            return selectedQuestions;
        }
    }
    
    评论

报告相同问题?

问题事件

  • 系统已结题 9月18日
  • 创建了问题 9月10日