跳到正文

wangxu101

1728084

我是王旭,一个爱编程的孩子。

在洛谷打开
资料、奖项与版本

资料

编号
1728084
抓取

版本

  1. v2当前
  2. v1

我是王旭,一个爱编程的孩子。

You can click it,it’s a surprise!

给你们听几首豆包AI的歌:

南京悲忆之殇

毕业青春回忆

C++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <windows.h>

using namespace std;

// 生成不重复4位随机号码
int generateUniqueNum(vector<int>& used)
{
    int num;
    while (true)
    {
        num = rand() % 9000 + 1000;
        if (find(used.begin(), used.end(), num) == used.end())
        {
            used.push_back(num);
            return num;
        }
    }
}

// 设置控制台颜色
void setColor(int color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

int main()
{
    srand((unsigned int)time(0));
    vector<int> usedNumbers;

    // 标题:白色字体 + 黑色背景 最醒目
    setColor(15);
    cout << endl;
    cout << "================================" << endl;
    cout << "          幸运抽奖系统          " << endl;
    cout << "================================" << endl;
    cout << endl;

    setColor(7);
    int userNum;
    cout << "请输入抽奖号码(1000 - 9999):";
    cin >> userNum;
    cout << endl;

    // 生成各奖项号码
    int special = generateUniqueNum(usedNumbers);
    int first1 = generateUniqueNum(usedNumbers);
    int first2 = generateUniqueNum(usedNumbers);

    int second1 = generateUniqueNum(usedNumbers);
    int second2 = generateUniqueNum(usedNumbers);
    int second3 = generateUniqueNum(usedNumbers);
    int second4 = generateUniqueNum(usedNumbers);

    int third1 = generateUniqueNum(usedNumbers);
    int third2 = generateUniqueNum(usedNumbers);
    int third3 = generateUniqueNum(usedNumbers);
    int third4 = generateUniqueNum(usedNumbers);
    int third5 = generateUniqueNum(usedNumbers);
    int third6 = generateUniqueNum(usedNumbers);
    int third7 = generateUniqueNum(usedNumbers);
    int third8 = generateUniqueNum(usedNumbers);

    // 开奖结果:黄色高亮文字
    setColor(14);
    cout << "================================" << endl;
    cout << "          开 奖 结 果          " << endl;
    cout << "================================" << endl;
    cout << endl;

    setColor(12); // 红色字
    cout << "特等奖:" << special << endl << endl;

    setColor(10); // 绿色字
    cout << "一等奖:" << first1 << "  " << first2 << endl << endl;

    setColor(9);  // 蓝色字
    cout << "二等奖:" << second1 << "  " << second2 << "  " 
         << second3 << "  " << second4 << endl << endl;

    setColor(13); // 紫色字
    cout << "三等奖:" << third1 << " " << third2 << " " << third3 << " " << third4
         << " " << third5 << " " << third6 << " " << third7 << " " << third8 << endl;
    cout << endl;

    setColor(15);
    cout << "================================" << endl;
    cout << endl;

    // 中奖提示
    if (userNum == special)
    {
        setColor(12);
        cout << "★★★ 恭 喜 中 特 等 奖 ★★★" << endl;
    }
    else if (userNum == first1 || userNum == first2)
    {
        setColor(10);
        cout << "★★ 恭 喜 中 一 等 奖 ★★" << endl;
    }
    else if (userNum == second1 || userNum == second2 || userNum == second3 || userNum == second4)
    {
        setColor(9);
        cout << "★ 恭 喜 中 二 等 奖 ★" << endl;
    }
    else if (userNum == third1 || userNum == third2 || userNum == third3 || userNum == third4 || userNum == third5 || userNum == third6 || userNum == third7 || userNum == third8)
    {
        setColor(13);
        cout << "★ 恭 喜 中 三 等 奖 ★" << endl;
    }
    else
    {
        setColor(8);
        cout << "很遗憾,本次未中奖,下次加油!" << endl;
    }

    cout << endl;
    setColor(7); // 恢复默认颜色
    system("pause"); // 窗口停留,防止一闪而过
    return 0;
}

还没有收录这位用户的文章。