跳到正文

算法·理论

如何在P1001 A+B Problem中凑齐RE,TLE,AC,WA,MLE

YANGZIMU2013约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月17日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

这不是题解!!!

挑战在P1001 A+B Problem中凑齐AC,WA,RE,TLE,MLE

  • 实现后结果类似如下图

PS:每种的概率为15\frac{1}{5}

实现方法

  • AC:正常输入和输出
  • WA:输出A+b+1A + b + 1
  • TLE:while (true)无限循环
  • MLE: 持续分配100MB大小的内存块
  • RE: 使用÷0÷0错误
  • 随机数生成:使用chrono::high_resolution_clock获取当前时间的微秒数作为随机种子保证在多个测试点几乎同时运行时不会出现完全一样的情况

上代码:

C++
#include <iostream>
#include <random>
#include <chrono>
#include <vector>

using namespace std;

int main() {
    auto now = chrono::high_resolution_clock::now();
    auto micros = chrono::time_point_cast<chrono::microseconds>(now);
    long seed = micros.time_since_epoch().count();

    mt19937 gen(seed);
    uniform_int_distribution<> dis(0, 4);
    int random = dis(gen);

    int a, b;
    cin >> a >> b;

    if (random == 0) {
        cout << a + b << endl;
    } else if (random == 1) {
        cout << a + b + 1 << endl;
    } else if (random == 2) {
        while (true) {}
    } else if (random == 3) {
        vector<char*> memory_blocks;
        try {
            while (true) {
                char* block = new char[100000000];
                memory_blocks.push_back(block);
            }
        } catch (const bad_alloc& e) {
            cout << a + b << endl;
        }
    } else {
        int x = 0;
        int y = 1 / x;
        cout << y << endl;
    }

    return 0;
}

再次申明:这不是题解!!!!!

这不是题解!!!!!

这不是题解!!!!!

原文