问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

C语言如何随机出十道题

创作时间:
作者:
@小白创作中心

C语言如何随机出十道题

引用
1
来源
1.
https://docs.pingcode.com/baike/1071195

本文将详细介绍如何使用C语言随机生成十道题目。通过引入随机数生成器、使用数组存储题库、确保题目不重复等多个步骤,最终实现一个完整的随机出题系统。文章还提供了优化建议和扩展思路,帮助读者更好地理解和应用相关技术。

要在C语言中随机出十道题,可以利用随机数生成器使用数组存储题库,并确保题目不重复。首先,我们需要一个题库,存储在数组中,然后使用随机数生成器来选择题目。为了确保题目不重复,可以使用标记数组或其他数据结构来记录已经选出的题目。

一、引入随机数生成器

在C语言中,
stdlib.h
库提供了随机数生成函数
rand()
,使用
rand()
可以生成一个范围内的随机数。为了确保每次运行程序时生成的随机数不同,可以使用
srand()
函数并结合时间函数
time(NULL)
进行初始化。

#include <stdlib.h>  
#include <time.h>  
void initialize_random() {  
    srand(time(NULL));  
}  

二、使用数组存储题库

题库可以用一个数组来存储,每个元素代表一道题目。假设我们有一个包含100道题目的题库:

#define TOTAL_QUESTIONS 100  
char *question_bank[TOTAL_QUESTIONS] = {  
    "Question 1", "Question 2", "Question 3", ..., "Question 100"  
};  

三、确保题目不重复

为了确保随机选择的题目不重复,可以使用一个辅助数组来记录已经选出的题目。每次生成一个随机数后,检查该题目是否已经被选出。如果已被选出,则重新生成一个随机数。

#define NUM_QUESTIONS 10  
void generate_random_questions() {  
    int selected[NUM_QUESTIONS];  
    int count = 0;  
    while (count < NUM_QUESTIONS) {  
        int rand_index = rand() % TOTAL_QUESTIONS;  
        int already_selected = 0;  
        for (int i = 0; i < count; i++) {  
            if (selected[i] == rand_index) {  
                already_selected = 1;  
                break;  
            }  
        }  
        if (!already_selected) {  
            selected[count++] = rand_index;  
        }  
    }  
    // Print selected questions  
    for (int i = 0; i < NUM_QUESTIONS; i++) {  
        printf("%sn", question_bank[selected[i]]);  
    }  
}  

四、完整示例代码

将上述步骤综合起来,形成一个完整的C程序:

#include <stdio.h>  
#include <stdlib.h>  
#include <time.h>  
#define TOTAL_QUESTIONS 100  
#define NUM_QUESTIONS 10  
void initialize_random() {  
    srand(time(NULL));  
}  
void generate_random_questions() {  
    char *question_bank[TOTAL_QUESTIONS] = {  
        "Question 1", "Question 2", "Question 3", /*...*/ "Question 100"  
    };  
    int selected[NUM_QUESTIONS];  
    int count = 0;  
    while (count < NUM_QUESTIONS) {  
        int rand_index = rand() % TOTAL_QUESTIONS;  
        int already_selected = 0;  
        for (int i = 0; i < count; i++) {  
            if (selected[i] == rand_index) {  
                already_selected = 1;  
                break;  
            }  
        }  
        if (!already_selected) {  
            selected[count++] = rand_index;  
        }  
    }  
    // Print selected questions  
    for (int i = 0; i < NUM_QUESTIONS; i++) {  
        printf("%sn", question_bank[selected[i]]);  
    }  
}  
int main() {  
    initialize_random();  
    generate_random_questions();  
    return 0;  
}  

五、优化与扩展

1、提高随机数生成的效率

使用洗牌算法(如Fisher-Yates算法)可以提高随机数生成的效率。这个算法能够在O(n)时间复杂度内生成一个无重复的随机排列。

void shuffle(int *array, int n) {  
    for (int i = n - 1; i > 0; i--) {  
        int j = rand() % (i + 1);  
        int temp = array[i];  
        array[i] = array[j];  
        array[j] = temp;  
    }  
}  
void generate_random_questions() {  
    int indices[TOTAL_QUESTIONS];  
    for (int i = 0; i < TOTAL_QUESTIONS; i++) {  
        indices[i] = i;  
    }  
    shuffle(indices, TOTAL_QUESTIONS);  
    for (int i = 0; i < NUM_QUESTIONS; i++) {  
        printf("%sn", question_bank[indices[i]]);  
    }  
}  

2、扩展题库管理

可以将题库从文件中读取,或者使用结构体存储更复杂的题目信息(如题目内容、选项、答案等)。

#include <string.h>  
typedef struct {  
    char content[256];  
    char options[4][64];  
    char answer;  
} Question;  
Question question_bank[TOTAL_QUESTIONS];  
void load_questions_from_file(const char *filename) {  
    FILE *file = fopen(filename, "r");  
    if (file) {  
        for (int i = 0; i < TOTAL_QUESTIONS; i++) {  
            fgets(question_bank[i].content, sizeof(question_bank[i].content), file);  
            for (int j = 0; j < 4; j++) {  
                fgets(question_bank[i].options[j], sizeof(question_bank[i].options[j]), file);  
            }  
            question_bank[i].answer = fgetc(file);  
            fgetc(file); // consume newline  
        }  
        fclose(file);  
    }  
}  
void generate_random_questions() {  
    int indices[TOTAL_QUESTIONS];  
    for (int i = 0; i < TOTAL_QUESTIONS; i++) {  
        indices[i] = i;  
    }  
    shuffle(indices, TOTAL_QUESTIONS);  
    for (int i = 0; i < NUM_QUESTIONS; i++) {  
        int index = indices[i];  
        printf("Q%d: %sn", i + 1, question_bank[index].content);  
        for (int j = 0; j < 4; j++) {  
            printf("%c. %sn", 'A' + j, question_bank[index].options[j]);  
        }  
        printf("Answer: %cn", question_bank[index].answer);  
    }  
}  

六、总结

通过上述步骤,可以在C语言中实现随机选择十道题目的功能。关键在于利用随机数生成器使用数组存储题库,以及确保题目不重复。优化和扩展部分还展示了更高效的随机选择算法和更复杂的题库管理方式。无论是简单的题库还是复杂的结构体题库,以上方法都可以灵活应对。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号