试卷生成算法问题请教!

liziyu

问题描述

一个班有50名学生要生成每人的考试答卷。

要求是:让随机的2人考95分以上、15人考80~90分、30人考60~80分、剩余的不及格;同时要求做到错题不能有完全一样的试卷。也就是说,同学要随机、分数要随机、错题也要随机。

题型为:单选、不定项选择与判断题,单题分值在1、2、3、4、5分布。

请问大佬们有没好办法,求指导。

-

为此你搜索到了哪些方案及不适用的原因

暂时没有搜索到此类似问题。

869 1 2
1个回答

super_zlq
<?php
// 题库
$questions = [
    ['type' => 'single', 'content' => '单选题1', 'score' => 1],
    ['type' => 'single', 'content' => '单选题2', 'score' => 1],
    ['type' => 'multiple', 'content' => '不定项选择题1', 'score' => 2],
    ['type' => 'multiple', 'content' => '不定项选择题2', 'score' => 2],
    ['type' => 'judge', 'content' => '判断题1', 'score' => 1],
    ['type' => 'judge', 'content' => '判断题2', 'score' => 1],
];

// 生成试卷
function generatePaper($questions, $numStudents) {
    $papers = [];

    // 随机生成2人考95分以上
    $topStudents = array_rand(range(0, $numStudents-1), 2);
    foreach ($topStudents as $student) {
        $papers[$student] = generatePaperForStudent($questions, 95);
    }

    // 随机生成15人考80~90分
    $middleStudents = array_rand(array_diff(range(0, $numStudents-1), $topStudents), 15);
    foreach ($middleStudents as $student) {
        $papers[$student] = generatePaperForStudent($questions, rand(80, 90));
    }

    // 随机生成剩余人数考60~80分
    $remainingStudents = array_diff(range(0, $numStudents-1), $topStudents, $middleStudents);
    foreach ($remainingStudents as $student) {
        $papers[$student] = generatePaperForStudent($questions, rand(60, 80));
    }

    // 随机生成错题
    foreach ($papers as &$paper) {
        $paper['wrongQuestions'] = generateWrongQuestions($questions);
    }

    return $papers;
}

// 为单个学生生成试卷
function generatePaperForStudent($questions, $score) {
    $paper = [
        'score' => $score,
        'questions' => [],
    ];

    $totalScore = 0;
    while ($totalScore < $score) {
        $question = $questions[array_rand($questions)];
        if ($totalScore + $question['score'] <= $score) {
            $paper['questions'][] = $question;
            $totalScore += $question['score'];
        }
    }

    return $paper;
}

// 生成错题
function generateWrongQuestions($questions) {
    $wrongQuestions = [];
    $numQuestions = count($questions);

    $numWrong = rand(1, $numQuestions);
    $wrongIndices = array_rand(range(0, $numQuestions-1), $numWrong);

    foreach ($wrongIndices as $index) {
        $wrongQuestions[] = $questions[$index];
    }

    return $wrongQuestions;
}

// 生成50名学生的试卷
$papers = generatePaper($questions, 50);

// 输出试卷
foreach ($papers as $student => $paper) {
    echo "学生" . ($student + 1) . "的试卷:\n";
    echo "总分:" . $paper['score'] . "分\n";
    echo "错题:\n";
    foreach ($paper['wrongQuestions'] as $question) {
        echo $question['content'] . "\n";
    }
    echo "\n";
}
  • liziyu 2023-07-18

    迟到的爱,还是要用心接受,非常感谢!!

年代过于久远,无法发表回答
×
🔝