dev-c++ 11/98上课随机点名代码

#include <iostream>
#include <vector>
#include <cstdlib> // For rand() and srand()
#include <ctime>   // For time()

using namespace std;

int main() {
    // 初始化随机数种子
    srand(time(0));

    // 定义一个包含52个学生名字的向量
    vector<string> students;
    students.push_back("张三");
    students.push_back("李四");
    students.push_back("王五");
    students.push_back("赵六");
    students.push_back("孙七");
    students.push_back("周八");
    students.push_back("吴九");
    students.push_back("郑十");
    students.push_back("刘十一");
    students.push_back("陈十二");
    students.push_back("杨十三");
    students.push_back("黄十四");
    students.push_back("高十五");
    students.push_back("林十六");
    students.push_back("何十七");
    students.push_back("郭十八");
    students.push_back("马十九");
    students.push_back("罗二十");
    students.push_back("梁二十一");
    students.push_back("宋二十二");
    students.push_back("谢二十三");
    students.push_back("韩二十四");
    students.push_back("唐二十五");
    students.push_back("冯二十六");
    students.push_back("于二十七");
    students.push_back("董二十八");
    students.push_back("萧二十九");
    students.push_back("程三十");
    students.push_back("曹三十一");
    students.push_back("袁三十二");
    students.push_back("邓三十三");
    students.push_back("许三十四");
    students.push_back("傅三十五");
    students.push_back("沈三十六");
    students.push_back("曾三十七");
    students.push_back("彭三十八");
    students.push_back("吕三十九");
    students.push_back("苏四十");
    students.push_back("卢四十一");
    students.push_back("蒋四十二");
    students.push_back("蔡四十三");
    students.push_back("贾四十四");
    students.push_back("丁四十五");
    students.push_back("魏四十六");
    students.push_back("薛四十七");
    students.push_back("叶四十八");
    students.push_back("阎四十九");
    students.push_back("余五十");
    students.push_back("潘五十一");
    students.push_back("杜五十二");

    // 已点名的学生向量
    vector<string> called;

    cout << "班级共有 " << students.size()+2 << " 名学生。\n";

    while (!students.empty()) {
        // 随机选择一名学生
        int index = rand() % students.size();
        string student = students[index];

        // 将这名学生从待抽取列表中移除,并加入到已点名列表
        students.erase(students.begin() + index);
        called.push_back(student);

        cout << "点名: " << student << endl;

        // 打印已点名和未点名的学生列表
        cout << "\n当前情况:" << endl;
        cout << "已点名学生:";
        for (vector<string>::iterator it = called.begin(); it != called.end(); ++it) {
            cout << *it << " ";
        }
        cout << "\n未点名学生:";
        for (vector<string>::iterator it = students.begin(); it != students.end(); ++it) {
            cout << *it << " ";
        }
        cout << "\n\n";

        // 暂停,等待用户按任意键继续
        cout << "请按任意键继续点名...\n";
        cin.get();
        cin.ignore();
    }

    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值