代码示例

#include<iostream>
#include<cstdlib>
#include<ctime>
int main()
{
    srand(static_cast<unsigned int>(time(nullptr))); // 设置随机数生成种子为当前时间
    int total = 100000; // 假设有100000人参加抽奖,中奖人数为199人
    int win = 199; // 中奖人数
    int* winners = new int[win]; // 保存中奖者的序号
    // 生成中奖者序号 
    for (int i = 0; i < win; ++i)
    {
        int idx = rand() % total;
        winners[i] = idx;
    }
    // 输出中奖者的序号
    std::cout << "Winners: ";
    for (int i = 0; i < win; ++i)
    {
        std::cout << winners[i] << " ";
    }
    std::cout << std::endl;
    delete[] winners;
    return 0;
}
最后修改:2023 年 08 月 04 日
如果觉得我的文章对你有用,请随意赞赏