跳到正文

休闲·娱乐

mini洛谷

Want_to_learn123约 30 分钟
原文
文章信息与版本

文章信息

发布
2026年5月3日
收录
2026年9月6日
更新
2026年11月12日
篇幅
约 30 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <vector>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <conio.h>
#include <windows.h>

using namespace std;

// ================= 数据结构 =================
struct User {
    char name[30];
    char pwd[30];
    int ac_count;
    int total_score;
};

struct Test {
    char input[100];
    char expected[100];
};

struct Problem {
    int id, difficulty;
    int timelimit, memlimit;
    char title[60], tags[80];
    int testCount;
    Test tests[3];
    int solved_count;
};

struct Submission {
    int id, uid, pid, score, time_ms, mem_kb;
    char verdict[5];
    char timestamp[20];
};

// ================= 全局状态 =================
vector<User> users;
vector<Problem> problems;
vector<Submission> subs;
int currentUserIdx = -1;
int subIdCounter = 1;

// ================= 工具函数 =================
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void setColor(int color) { SetConsoleTextAttribute(hConsole, color); }
void clearInput() { cin.clear(); cin.ignore(1000, '\n'); }
void cls() { system("cls"); }
void pause() { setColor(8); cout << "\n[按任意键返回]"; getch(); setColor(15); cls(); }

const int C_DEF=15, C_GREEN=10, C_RED=12, C_BLUE=9, C_CYAN=11, C_YELLOW=14, C_GRAY=8;

// ================= 数据持久化 =================
void saveData() {
    ofstream fout("luogu_users.dat", ios::trunc);
    for(size_t i=0; i<users.size(); i++) {
        fout << users[i].name << " " << users[i].pwd << " " 
             << users[i].ac_count << " " << users[i].total_score << endl;
    }
    fout.close();

    fout.open("luogu_subs.dat", ios::trunc);
    for(size_t i=0; i<subs.size(); i++) {
        fout << subs[i].id << " " << subs[i].uid << " " << subs[i].pid << " "
             << subs[i].score << " " << subs[i].time_ms << " " << subs[i].mem_kb << " "
             << subs[i].verdict << " " << subs[i].timestamp << endl;
    }
    fout.close();
}

void loadData() {
    ifstream fin("luogu_users.dat");
    if(fin.is_open()) {
        User u;
        while(fin >> u.name >> u.pwd >> u.ac_count >> u.total_score) users.push_back(u);
        fin.close();
    }
    fin.open("luogu_subs.dat");
    if(fin.is_open()) {
        Submission s;
        while(fin >> s.id >> s.uid >> s.pid >> s.score >> s.time_ms >> s.mem_kb >> s.verdict >> s.timestamp) subs.push_back(s);
        fin.close();
    }
}

// ================= 核心业务 =================
void initProblems() {
    if(!problems.empty()) return;
    Problem p;
    memset(&p, 0, sizeof(p)); // ? 安全清空结构体

    // P1000
    p.id=1000; p.difficulty=1; p.timelimit=1000; p.memlimit=128;
    strcpy(p.title,"A+B Problem"); strcpy(p.tags,"入门 模拟");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"1 2"); strcpy(p.tests[0].expected,"3");
    strcpy(p.tests[1].input,"-5 10"); strcpy(p.tests[1].expected,"5");
    problems.push_back(p);

    // P1001
    memset(&p, 0, sizeof(p));
    p.id=1001; p.difficulty=1; p.timelimit=1000; p.memlimit=128;
    strcpy(p.title,"Hello World"); strcpy(p.tags,"入门 字符串");
    p.testCount=1; p.solved_count=0;
    strcpy(p.tests[0].input,""); strcpy(p.tests[0].expected,"Hello World!");
    problems.push_back(p);

    // P1002
    memset(&p, 0, sizeof(p));
    p.id=1002; p.difficulty=2; p.timelimit=500; p.memlimit=64;
    strcpy(p.title,"求最大值"); strcpy(p.tags,"分支 排序");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"3 5 1"); strcpy(p.tests[0].expected,"5");
    strcpy(p.tests[1].input,"-2 -8 -1"); strcpy(p.tests[1].expected,"-1");
    problems.push_back(p);

    // P1003
    memset(&p, 0, sizeof(p));
    p.id=1003; p.difficulty=1; p.timelimit=1000; p.memlimit=64;
    strcpy(p.title,"阶乘计算"); strcpy(p.tags,"入门 循环");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"3"); strcpy(p.tests[0].expected,"9");
    strcpy(p.tests[1].input,"4"); strcpy(p.tests[1].expected,"33");
    problems.push_back(p);

    // P1004
    memset(&p, 0, sizeof(p));
    p.id=1004; p.difficulty=2; p.timelimit=1000; p.memlimit=64;
    strcpy(p.title,"回文判断"); strcpy(p.tags,"入门 字符串");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"12321"); strcpy(p.tests[0].expected,"YES");
    strcpy(p.tests[1].input,"abcba"); strcpy(p.tests[1].expected,"YES");
    problems.push_back(p);

    // P1005
    memset(&p, 0, sizeof(p));
    p.id=1005; p.difficulty=2; p.timelimit=1000; p.memlimit=128;
    strcpy(p.title,"质数计数"); strcpy(p.tags,"普及- 数论");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"10"); strcpy(p.tests[0].expected,"4");
    strcpy(p.tests[1].input,"20"); strcpy(p.tests[1].expected,"8");
    problems.push_back(p);

    // P1006
    memset(&p, 0, sizeof(p));
    p.id=1006; p.difficulty=2; p.timelimit=1000; p.memlimit=128;
    strcpy(p.title,"数组排序"); strcpy(p.tags,"普及- 排序");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"3 1 2"); strcpy(p.tests[0].expected,"1 2 3");
    strcpy(p.tests[1].input,"5 4 3 2 1"); strcpy(p.tests[1].expected,"1 2 3 4 5");
    problems.push_back(p);

    // P1007
    memset(&p, 0, sizeof(p));
    p.id=1007; p.difficulty=3; p.timelimit=500; p.memlimit=64;
    strcpy(p.title,"斐波那契"); strcpy(p.tags,"普及 递推");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"5"); strcpy(p.tests[0].expected,"5");
    strcpy(p.tests[1].input,"7"); strcpy(p.tests[1].expected,"13");
    problems.push_back(p);

    // P1008
    memset(&p, 0, sizeof(p));
    p.id=1008; p.difficulty=3; p.timelimit=1000; p.memlimit=128;
    strcpy(p.title,"矩阵转置"); strcpy(p.tags,"普及/提高 模拟");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"1 2 3 4"); strcpy(p.tests[0].expected,"1 3 2 4");
    strcpy(p.tests[1].input,"1 2 3 4 5 6"); strcpy(p.tests[1].expected,"1 4 2 5 3 6");
    problems.push_back(p);

    // P1009
    memset(&p, 0, sizeof(p));
    p.id=1009; p.difficulty=4; p.timelimit=500; p.memlimit=256;
    strcpy(p.title,"最大子段和"); strcpy(p.tags,"提高 动态规划");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"3 -2 4 -1 2"); strcpy(p.tests[0].expected,"5");
    strcpy(p.tests[1].input,"-1 -2 -3"); strcpy(p.tests[1].expected,"-1");
    problems.push_back(p);

    // P1010
    memset(&p, 0, sizeof(p));
    p.id=1010; p.difficulty=4; p.timelimit=1000; p.memlimit=128;
    strcpy(p.title,"字符串匹配"); strcpy(p.tags,"提高 KMP");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"hello world world"); strcpy(p.tests[0].expected,"2");
    strcpy(p.tests[1].input,"abc abc abc"); strcpy(p.tests[1].expected,"3");
    problems.push_back(p);

    // P1011
    memset(&p, 0, sizeof(p));
    p.id=1011; p.difficulty=5; p.timelimit=2000; p.memlimit=512;
    strcpy(p.title,"01背包问题"); strcpy(p.tags,"省选 动态规划");
    p.testCount=2; p.solved_count=0;
    strcpy(p.tests[0].input,"10 3 5 10 6 8 4 5"); strcpy(p.tests[0].expected,"15");
    strcpy(p.tests[1].input,"5 2 2 3 3 4"); strcpy(p.tests[1].expected,"7");
    problems.push_back(p);
}

void registerUser() {
    cls(); setColor(C_CYAN); cout << "=== 注册新账号 ===\n"; setColor(C_DEF);
    User newUser;
    cout << "用户名: "; cin >> newUser.name;
    for(size_t i=0; i<users.size(); i++) {
        if(strcmp(newUser.name, users[i].name) == 0) {
            setColor(C_RED); cout << "? 该用户名已被注册!\n"; setColor(C_DEF); pause(); return;
        }
    }
    cout << "密  码: "; cin >> newUser.pwd;
    clearInput();
    newUser.ac_count = 0; newUser.total_score = 0;
    users.push_back(newUser);
    currentUserIdx = users.size() - 1;
    setColor(C_GREEN); cout << "? 注册成功并已自动登录!\n"; setColor(C_DEF); saveData(); pause();
}

void login() {
    cls(); setColor(C_BLUE); cout << "=== 用户登录 ===\n"; setColor(C_DEF);
    char uName[30], uPwd[30];
    cout << "用户名: "; cin >> uName;
    cout << "密  码: "; cin >> uPwd;
    clearInput();
    currentUserIdx = -1;
    for(size_t i=0; i<users.size(); i++) {
        if(strcmp(users[i].name, uName)==0 && strcmp(users[i].pwd, uPwd)==0) { currentUserIdx = i; break; }
    }
    if(currentUserIdx != -1) { setColor(C_GREEN); cout << "? 登录成功!欢迎回来, " << users[currentUserIdx].name << "\n"; }
    else { setColor(C_RED); cout << "? 账号或密码错误。\n"; }
    setColor(C_DEF); pause();
}

void showProblems() {
    cls(); setColor(C_YELLOW);
    cout << "================ 题 目 库 (共" << problems.size() << "题) ================\n"; setColor(C_DEF);
    cout << " 编号 | 难度 | 题目名称          | 标签        | 通过人数\n";
    cout << "------|------|-------------------|-------------|--------\n";
    for(size_t i=0; i<problems.size(); i++) {
        setColor(C_BLUE); cout << " P" << problems[i].id;
        setColor(C_YELLOW); 
        for(int d=0; d<problems[i].difficulty; d++) cout << "★";
        setColor(C_DEF); cout << "  ";
        setColor(C_GREEN); cout << problems[i].title;
        setColor(C_CYAN); 
        int pad = 20 - strlen(problems[i].title);
        for(int k=0;k<pad;k++) cout << " ";
        setColor(C_GRAY); cout << problems[i].tags << "      ";
        setColor(C_DEF); cout << problems[i].solved_count << endl;
    }
    cout << "========================================================\n";
    pause();
}

void submitProblem() {
    if(currentUserIdx == -1) { setColor(C_RED); cout << "?? 请先登录!"; setColor(C_DEF); pause(); return; }
    cls(); setColor(C_CYAN); cout << "请输入题目编号 (例: 1000): "; setColor(C_DEF);
    int pid; cin >> pid; clearInput();
    int pIdx = -1;
    for(size_t i=0; i<problems.size(); i++) if(problems[i].id == pid) { pIdx = i; break; }
    if(pIdx == -1) { setColor(C_RED); cout << "? 题目不存在!"; setColor(C_DEF); pause(); return; }
    
    Problem &p = problems[pIdx];
    cls(); setColor(C_BLUE);
    cout << "?? P" << p.id << " - " << p.title << "\n";
    cout << "? 限时: " << p.timelimit << "ms | ?? 限内存: " << p.memlimit << "MB\n\n";
    
    bool allPass = true; int totalTime = 0; srand((unsigned)time(NULL));
    for(int i=0; i<p.testCount; i++) {
        cout << "? 测试点 " << (i+1) << "/" << p.testCount << "\n";
        cout << "   输入: " << p.tests[i].input << "\n";
        cout << "   你的输出: ";
        char ans[100]; cin.getline(ans, 100); clearInput();
        int simTime = rand() % (p.timelimit/2) + 10;
        Sleep(simTime / 5); totalTime += simTime;
        if(totalTime > p.timelimit) { cout << "   ? Time Limit Exceeded\n"; allPass=false; break; }
        if(strcmp(ans, p.tests[i].expected) == 0) { setColor(C_GREEN); cout << "   ? Accepted (" << simTime << "ms)\n"; setColor(C_DEF); }
        else { setColor(C_RED); cout << "   ? Wrong Answer\n"; cout << "   期望: " << p.tests[i].expected << "\n   你的: " << ans << "\n"; setColor(C_DEF); allPass=false; break; }
    }
    
    Submission s;
    s.id = subIdCounter++; s.uid = currentUserIdx; s.pid = pid; s.time_ms = totalTime; s.mem_kb = rand()%512 + 64;
    time_t now = time(NULL); struct tm* t = localtime(&now); sprintf(s.timestamp, "%02d:%02d", t->tm_hour, t->tm_min);
    if(allPass) { strcpy(s.verdict, "AC"); s.score = 100; problems[pIdx].solved_count++; users[currentUserIdx].ac_count++; users[currentUserIdx].total_score += 100; }
    else if(totalTime > p.timelimit) { strcpy(s.verdict, "TLE"); s.score = 0; }
    else { strcpy(s.verdict, "WA"); s.score = 0; }
    subs.push_back(s); saveData();
    cls(); setColor(C_CYAN); cout << "================ 评测结果 =================\n";
    cout << " 状态: " << s.verdict << " | 得分: " << s.score << "/100\n";
    cout << " 用时: " << s.time_ms << "ms | 内存: " << s.mem_kb << "KB\n";
    cout << "==========================================\n"; pause();
}

void showRanklist() {
    cls(); setColor(C_YELLOW); cout << "================ 排行榜 ==================\n"; setColor(C_DEF);
    cout << " 排名 | 用户名        | AC题数 | 总得分\n"; cout << "------|--------------|--------|-------\n";
    vector<int> idx; for(size_t i=0; i<users.size(); i++) idx.push_back(i);
    for(size_t i=0; i<idx.size(); i++) for(size_t j=i+1; j<idx.size(); j++) {
        if(users[idx[j]].ac_count > users[idx[i]].ac_count || (users[idx[j]].ac_count == users[idx[i]].ac_count && users[idx[j]].total_score > users[idx[i]].total_score)) { int temp=idx[i]; idx[i]=idx[j]; idx[j]=temp; }
    }
    for(size_t i=0; i<idx.size(); i++) {
        User &u = users[idx[i]]; setColor(C_BLUE); cout << " #" << (i+1); setColor(C_GREEN); cout << " | " << u.name;
        int pad = 14 - strlen(u.name); for(int k=0;k<pad;k++) cout << " ";
        setColor(C_YELLOW); cout << " | " << u.ac_count << "     | " << u.total_score << "\n"; setColor(C_DEF);
    }
    cout << "==========================================\n"; pause();
}

void showHistory() {
    if(currentUserIdx == -1) { setColor(C_RED); cout << "?? 请先登录!"; setColor(C_DEF); pause(); return; }
    cls(); setColor(C_YELLOW); cout << "================ 我的提交记录 ==============\n"; setColor(C_DEF);
    cout << " ID   | 题号 | 状态 | 得分 | 用时(ms) | 内存(KB) | 时间\n"; cout << "------|------|------|------|----------|----------|--------\n";
    bool hasData = false;
    for(int i=subs.size()-1; i>=0; i--) if(subs[i].uid == currentUserIdx) {
        hasData = true; setColor(C_BLUE); cout << " " << subs[i].id; setColor(C_DEF); cout << " | P" << subs[i].pid << " ";
        int color = (strcmp(subs[i].verdict,"AC")==0)?C_GREEN:C_RED; setColor(color); cout << subs[i].verdict << "  ";
        setColor(C_YELLOW); cout << subs[i].score << "   | "; setColor(C_DEF); cout << subs[i].time_ms << "       | " << subs[i].mem_kb << "      | " << subs[i].timestamp << endl;
    }
    if(!hasData) { setColor(C_GRAY); cout << "?? 暂无提交记录。\n"; setColor(C_DEF); }
    cout << "============================================\n"; pause();
}

int main() {
    system("title 迷你洛谷 OJ 控制台版 v3.0");
    initProblems(); loadData(); srand((unsigned)time(NULL));
    while(true) {
        cls(); setColor(C_CYAN); cout << "╔══════════════════════════════════╗\n";
        cout << "║       ?? 迷你洛谷 (Mini OJ)      ║\n"; cout << "╠══════════════════════════════════╣\n";
        if(currentUserIdx == -1) { setColor(C_YELLOW); cout << "║  [1] 注册账号                    ║\n"; cout << "║  [2] 登录账号                    ║\n"; }
        else { setColor(C_GREEN); cout << "║  [已登录: " << users[currentUserIdx].name << "]              ║\n"; setColor(C_YELLOW); cout << "║  [0] 退出登录                    ║\n"; }
        setColor(C_YELLOW); cout << "║  [3] 查看题库                      ║\n"; cout << "║  [4] 提交代码                      ║\n"; cout << "║  [5] 提交记录                      ║\n"; cout << "║  [6] 排行榜                        ║\n"; cout << "║  [7] 退出系统                      ║\n"; cout << "╚══════════════════════════════════╝\n";
        setColor(C_DEF); cout << "请选择: "; char ch = getch();
        switch(ch) {
            case '0': if(currentUserIdx!=-1) { currentUserIdx=-1; setColor(C_GREEN); cout<<"已退出登录。"; pause(); } break;
            case '1': if(users.size()<10) registerUser(); else { setColor(C_RED); cout<<"?? 演示版限制:最多10个账号"; pause(); } break;
            case '2': login(); break; case '3': showProblems(); break; case '4': submitProblem(); break;
            case '5': showHistory(); break; case '6': showRanklist(); break;
            case '7': saveData(); cls(); setColor(C_GREEN); cout<<"?? 感谢使用!数据已保存。\n"; setColor(C_DEF); return 0;
            default: break;
        }
    }
    return 0;
}
原文