日期:2014-05-20  浏览次数:20695 次

关于随机生成指定分值的试卷问题
题库里有三种题型,选择,判断,填空。而每道题都有各自的分值,即同样是选择题也有1,2,3分的差别。

现在要指定每种题型的题目数量来组成试卷,还指定试卷的总分,比如100分。

假定题库不会出现题目不够的情况,求好点的思路,能贴出相关代码最好~

谢谢


------解决方案--------------------
思路,获取总题目数,然后先每个题目设置1分,然后看100-总分后还剩多少分
循环剩余分数,随机取1道题加1分,如果加到最大3分,则排除出下一次随机,直到剩余分数用完
最后再把所有的题目随机打乱一次顺序

for example
Java code
public static void randomTest(int[] tests) {
    if (tests == null || tests.length == 0) {
        System.out.println("error");
        return;
    }

    int sum = 0;
    for (int i : tests) {
        sum += i;
    }

    if (sum > 100) {
        System.out.println("error");
        return;
    }

    List<Integer> questions = new LinkedList<Integer>(); //题目
    List<Integer> maxScore = new ArrayList<Integer>(); //最大分数
    for (int i=0; i<sum; i++) {
        questions.add(1);
    }

    int remain = 100 - sum;
    while (remain > 0) { //剩余分数还有
        int s = questions.remove((int)Math.random()*(questions.size()));
        s++; //随机取一道题加1分
        if (s == 3) { //如果分数最大,则排除下一次随机
            maxScore.add(s);
        } else { //否则继续参与下一次随机
            questions.add(s);
        }
        remain--;
    }

    questions.addAll(maxScore); //获得所有题目
    Collections.shuffle(questions); //随机排列所有题目

    for (int i=0; i<sum; i++) { //打印题目
        if (i < tests[0]) {
            System.out.printf("select question, score[%d]\n", questions.get(i));
        } else if (tests.length > 1 && i < tests[0] + tests[1]) {
            System.out.printf("judgment question, score[%d]\n", questions.get(i));
        } else {
            System.out.printf("filling blank question, score[%d]\n", questions.get(i));
        }
    }
}

------解决方案--------------------
每种类型的题目都是固定个数,每种类型在题库里面随机抽取
------解决方案--------------------
思路,获取总题目数,然后先每个题目设置1分,然后看100-总分后还剩多少分
循环剩余分数,随机取1道题加1分,如果加到最大3分,则排除出下一次随机,直到剩余分数用完
最后再把所有的题目随机打乱一次顺序

for example

Java code

public static void randomTest(int[] tests) { if (tests == null || tests.length == 0) { System.out.println("error"); return; } int sum = 0; for (int i : tests) { sum += i; } if (sum > 100) { System.out.println("error"); return; } List<Integer> questions = new LinkedList<Integer>(); //题目 List<Integer> maxScore = new ArrayList<Integer>(); //最大分数 for (int i=0; i<sum; i++) { questions.add(1); } int remain = 100 - sum; while (remain > 0) { //剩余分数还有 int s = questions.remove((int)Math.random()*(questions.size())); s++; //随机取一道题加1分 if (s == 3) { //如果分数最大,则排除下一次随机 maxScore.add(s); } else { //否则继续参与下一次随机 questions.add(s); } remain--; } questions.addAll(maxScore); //获得所有题目 Collections.shuffle(questions); //随机排列所有题目 for (int i=0; i<sum; i++) { //打印题目 if (i < tests[0]) { System.out.printf("select question, score[%d]\n", questions.get(i)); } else if (tests.length > 1 && i < tests[0] + tests[1]) { System.out.printf("judgment question, score[%d]\n", questions.get(i)); } else { System.out.printf("filling blank question, score[%d]\n", questions.get(i)); } } }