跳到正文

个人记录

造数据代码使用教程

AC_Simulator约 29 分钟
原文
文章信息与版本

文章信息

收录
2026年1月1日
更新
2026年5月6日
篇幅
约 29 分钟

版本

  1. v3当前
  2. v2
  3. v1

0 前言

都 2025 20262026 年了,还有人不会造数据?

完整模板
C++
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>
#include <stdexcept>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define int long long
// #define cin fin
// #define cout fout
#define upl(i, s, t, d) for(int i = (s); i <= (t); i += (d))
#define dol(i, s, t, d) for(int i = (s); i >= (t); i -= (d))

void INIT() {
	#define RUN
	#ifdef RUN
	ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
	#endif
}

#ifdef RUN
void debug() {} template <typename... Args> void debug(Args&&...) {}
#else
void debug() {cout << "[Debug]\n"; } template <typename T, typename... Args>
void debug(T&& f, Args&&... args) {cout << forward<T>(f) << " "; debug(forward<Args>(args)...); }
#endif

const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 10;
const string LOW = "abcdefghijklmnopqrstuvwxyz", UPP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", NUM = "0123456789";
const string IN = ".in", OUT = ".out", FOLDER = "data/", NAME = "";

static mt19937_64& get_rng() {
    static random_device rd;
    static mt19937_64 rng(
        rd() ^ static_cast<unsigned long long>(chrono::high_resolution_clock::now().time_since_epoch().count())
    );
    return rng;
}
ll rand_ll(ll min, ll max) {
	if (min > max)
		throw invalid_argument("rand_ll:传入的 min > max!");
	uniform_int_distribution<ll> dist(min, max);
	return dist(get_rng());
}
ull rand_ull(ull min, ull max) {
	if (min > max)
		throw invalid_argument("rand_ull:传入的 min > max!");
	uniform_int_distribution<ull> dist(min, max);
	return dist(get_rng());
}
double rand_double(double min, double max) {
	if (min > max)
		throw invalid_argument("rand_double:传入的 min > max!");
	uniform_real_distribution<double> dist(min, max);
	return dist(get_rng());
}
char rand_char(const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_char:传入的 chatset 为空!");
	auto idx = rand_ull(0, static_cast<ull>(charset.size() - 1));
	return charset[static_cast<size_t>(idx)];
}
string rand_string(size_t length, const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_string:传入的 charset 为空!");
	if (length == 0)
		return "";
	string result;
	result.reserve(length);
	for (size_t i = 0; i < length; i++)
		result += rand_char(charset);
	return result;
}
template <typename RandomIt>
void rand_shuffle(RandomIt first, RandomIt last) {
	shuffle(first, last, get_rng());
}

ifstream fin;
ofstream fout;
void open_file(string name, string mod) {
	if(mod == "read") {
		if(fin.is_open())
			fin.close();
		fin.open(name);
		if(!fin.is_open())
			throw runtime_error("open_file(read 模式):文件 " + name + " 打开失败!");
	} else if(mod == "write") {
		if(fout.is_open()) fout.close();
		fout.open(name);
		if(!fout.is_open())
			throw runtime_error("open_file(write 模式):文件 " + name + " 打开失败!");
	} else {
		throw invalid_argument("open_file:传入的 mod 无效!应为 read/write。");
	}
}

signed main() {
	INIT();

	return 0;
}

1 初始化部分

1.1 头文件

C++
#include <iostream> // 不必过多解释了吧?
#include <fstream> // 文件读写
#include <string> // 不必过多解释了吧?
#include <algorithm> // shuffle
#include <random> // 随机数
#include <chrono> // 高精度时钟
#include <stdexcept> // 错误反馈

1.2 加速编写

C++
using namespace std; // 众所周知的 std
using ll = long long; // 等价于 typedef long long ll;
using ull = unsigned long long;
// #define int long long // 不开 long long 见祖宗(bushi)
// #define cin fin
// #define cout fout
#define upl(i, s, t, d) for(int i = (s); i <= (t); i += (d)) // 循环宏
#define dol(i, s, t, d) for(int i = (s); i >= (t); i -= (d))

1.3 void INIT() 初始化

C++
void INIT() {
	#define RUN // 此行若被注释,则代码进入调试状态(具体见下文)
	#ifdef RUN // 仅在非调试状态下启用加速
	ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); // cin/cout 加速
	#endif
}

1.4 debug 调试

C++
#ifdef RUN // 非调试状态下,debug 函数定义为空函数
void debug() {} template <typename... Args> void debug(Args&&...) {}
#else // 调试状态下才启用
void debug() {cout << "[Debug]\n"; } template <typename T, typename... Args>
void debug(T&& f, Args&&... args) {cout << forward<T>(f) << " "; debug(forward<Args>(args)...); }
#endif
  • 用法: debug(a, b, c...)。
  • 功能:debug 函数将传入的所有参数(数量不限、类型不限)输出,空格隔开,结尾输出 [Debug] 标签并换行。

1.5 常量

C++
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const ll LLINF = 0x3f3f3f3f3f3f3f3f; // 常用的临界值和模数
const int MAXN = 1e5 + 10; // 常用数组大小
const string LOW = "abcdefghijklmnopqrstuvwxyz", UPP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", NUM = "0123456789"; // 常用字符集
const string IN = ".in", OUT = ".out", FOLDER = "data/", NAME = ""; // 文件后缀、文件夹、题目名称(看你要不要用)

2 随机函数

2.1 static mt19937_64& get_rng()

C++
static mt19937_64& get_rng() {
    static random_device rd;
    static mt19937_64 rng(
        rd() ^ static_cast<unsigned long long>(chrono::high_resolution_clock::now().time_since_epoch().count())
    );
    return rng;
}
  • 功能:线程安全的、延迟初始化的全局高质量随机数生成器(RNG)单例。
INFO

这就是个初始化函数,你用不上。

2.2 ll rand_ll(ll min, ll max)

C++
ll rand_ll(ll min, ll max) {
	if (min > max)
		throw invalid_argument("rand_ll:传入的 min > max!");
	uniform_int_distribution<ll> dist(min, max);
	return dist(get_rng());
}
  • 功能:生成并返回在 [a,b][a, b] 中的随机 long long 类型数据。
  • 错误反馈:若 min>maxmin > max,则抛出异常并终止程序。

2.3 ull rand_ull(ull min, ull max)

C++
ull rand_ull(ull min, ull max) {
	if (min > max)
		throw invalid_argument("rand_ull:传入的 min > max!");
	uniform_int_distribution<ull> dist(min, max);
	return dist(get_rng());
}
  • 功能:生成并返回在 [a,b][a, b] 中的随机 unsigned long long 类型数据。
  • 错误反馈:若 min>maxmin > max,则抛出异常并终止程序。

2.4 double rand_double(double min, double max)

C++
double rand_double(double min, double max) {
	if (min > max)
		throw invalid_argument("rand_double:传入的 min > max!");
	uniform_real_distribution<double> dist(min, max);
	return dist(get_rng());
}
  • 功能:生成并返回在 [a,b)[a, b) 中的随机 double 类型数据。
  • 错误反馈:若 min>maxmin > max,则抛出异常并终止程序。

2.5 char rand_char(const string& charset)

C++
char rand_char(const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_char:传入的 chatset 为空!");
	auto idx = rand_ull(0, static_cast<ull>(charset.size() - 1));
	return charset[static_cast<size_t>(idx)];
}
  • 功能:生成并返回在 chersetcherset 中的 11 个随机字符(char)。
  • 错误反馈:若 charsetcharset 为空,则抛出异常并终止程序。

2.6 string rand_string(size_t length, const string& charset)

C++
string rand_string(size_t length, const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_string:传入的 charset 为空!");
	if (length == 0)
		return "";
	string result;
	result.reserve(length);
	for (size_t i = 0; i < length; i++)
		result += rand_char(charset);
	return result;
}
  • 功能:生成并返回长度为 lengthlength 的随机字符串(string),字符从 charsetcharset 中选。
  • 错误反馈:若 charsetcharset 为空,则抛出异常并终止程序。

2.7 void rand_shuffle(RandomIt first, RandomIt last)

C++
template <typename RandomIt>
void rand_shuffle(RandomIt first, RandomIt last) {
	shuffle(first, last, get_rng());
}
  • 功能:随机打乱 [first,last)[first, last) 之间的元素,支持任意类型。

3 文件读写(void open_file(string name, string mod) 函数)

C++
ifstream fin;
ofstream fout;
void open_file(string name, string mod) {
	if(mod == "read") {
		if(fin.is_open())
			fin.close();
		fin.open(name);
		if(!fin.is_open())
			throw runtime_error("open_file(read 模式):文件 " + name + " 打开失败!");
	} else if(mod == "write") {
		if(fout.is_open()) fout.close();
		fout.open(name);
		if(!fout.is_open())
			throw runtime_error("open_file(write 模式):文件 " + name + " 打开失败!");
	} else {
		throw invalid_argument("open_file:传入的 mod 无效!应为 read/write。");
	}
}
  • 功能:以 mod 模式,打开文件 name。
    • 读取模式(mod=readmod = \text{read}):从文件中读取数据(fin >>)。
    • 写入模式(mod=writemod = \text{write}):写入到文件(fout <<)。
  • 错误反馈:若打开文件失败或传入的 mod 无效,则抛出异常并终止程序。

4 示例(生成 P1001 A+B Problem 的数据)

较模板修改的部分已高亮。

生成输入数据的完整代码
C++
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>
#include <stdexcept>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define int long long
// #define cin fin
// #define cout fout
#define upl(i, s, t, d) for(int i = (s); i <= (t); i += (d))
#define dol(i, s, t, d) for(int i = (s); i >= (t); i -= (d))

void INIT() {
	#define RUN
	#ifdef RUN
	ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
	#endif
}

#ifdef RUN
void debug() {} template <typename... Args> void debug(Args&&...) {}
#else
void debug() {cout << "[Debug]\n"; } template <typename T, typename... Args>
void debug(T&& f, Args&&... args) {cout << forward<T>(f) << " "; debug(forward<Args>(args)...); }
#endif

const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 10;
const string LOW = "abcdefghijklmnopqrstuvwxyz", UPP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", NUM = "0123456789";
const string IN = ".in", OUT = ".out", FOLDER = "data/", NAME = "P1001";

static mt19937_64& get_rng() {
    static random_device rd;
    static mt19937_64 rng(
        rd() ^ static_cast<unsigned long long>(chrono::high_resolution_clock::now().time_since_epoch().count())
    );
    return rng;
}
ll rand_ll(ll min, ll max) {
	if (min > max)
		throw invalid_argument("rand_ll:传入的 min > max!");
	uniform_int_distribution<ll> dist(min, max);
	return dist(get_rng());
}
ull rand_ull(ull min, ull max) {
	if (min > max)
		throw invalid_argument("rand_ull:传入的 min > max!");
	uniform_int_distribution<ull> dist(min, max);
	return dist(get_rng());
}
double rand_double(double min, double max) {
	if (min > max)
		throw invalid_argument("rand_double:传入的 min > max!");
	uniform_real_distribution<double> dist(min, max);
	return dist(get_rng());
}
char rand_char(const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_char:传入的 chatset 为空!");
	auto idx = rand_ull(0, static_cast<ull>(charset.size() - 1));
	return charset[static_cast<size_t>(idx)];
}
string rand_string(size_t length, const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_string:传入的 charset 为空!");
	if (length == 0)
		return "";
	string result;
	result.reserve(length);
	for (size_t i = 0; i < length; i++)
		result += rand_char(charset);
	return result;
}
template <typename RandomIt>
void rand_shuffle(RandomIt first, RandomIt last) {
	shuffle(first, last, get_rng());
}

ifstream fin;
ofstream fout;
void open_file(string name, string mod) {
	if(mod == "read") {
		if(fin.is_open())
			fin.close();
		fin.open(name);
		if(!fin.is_open())
			throw runtime_error("open_file(read 模式):文件 " + name + " 打开失败!");
	} else if(mod == "write") {
		if(fout.is_open()) fout.close();
		fout.open(name);
		if(!fout.is_open())
			throw runtime_error("open_file(write 模式):文件 " + name + " 打开失败!");
	} else {
		throw invalid_argument("open_file:传入的 mod 无效!应为 read/write。");
	}
}

signed main() {
	INIT();
	upl(i, 1, 10, 1) {
		open_file(FOLDER + NAME + "_" + to_string(i) + IN, "write");
		fout << rand_ll(-1e9, 1e9) << " " << rand_ll(-1e9, 1e9);
	}
	return 0;
}
生成输出数据的完整代码
C++
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>
#include <stdexcept>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define int long long
// #define cin fin
// #define cout fout
#define upl(i, s, t, d) for(int i = (s); i <= (t); i += (d))
#define dol(i, s, t, d) for(int i = (s); i >= (t); i -= (d))

void INIT() {
	#define RUN
	#ifdef RUN
	ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
	#endif
}

#ifdef RUN
void debug() {} template <typename... Args> void debug(Args&&...) {}
#else
void debug() {cout << "[Debug]\n"; } template <typename T, typename... Args>
void debug(T&& f, Args&&... args) {cout << forward<T>(f) << " "; debug(forward<Args>(args)...); }
#endif

const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 10;
const string LOW = "abcdefghijklmnopqrstuvwxyz", UPP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", NUM = "0123456789";
const string IN = ".in", OUT = ".out", FOLDER = "data/", NAME = "P1001";

static mt19937_64& get_rng() {
    static random_device rd;
    static mt19937_64 rng(
        rd() ^ static_cast<unsigned long long>(chrono::high_resolution_clock::now().time_since_epoch().count())
    );
    return rng;
}
ll rand_ll(ll min, ll max) {
	if (min > max)
		throw invalid_argument("rand_ll:传入的 min > max!");
	uniform_int_distribution<ll> dist(min, max);
	return dist(get_rng());
}
ull rand_ull(ull min, ull max) {
	if (min > max)
		throw invalid_argument("rand_ull:传入的 min > max!");
	uniform_int_distribution<ull> dist(min, max);
	return dist(get_rng());
}
double rand_double(double min, double max) {
	if (min > max)
		throw invalid_argument("rand_double:传入的 min > max!");
	uniform_real_distribution<double> dist(min, max);
	return dist(get_rng());
}
char rand_char(const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_char:传入的 chatset 为空!");
	auto idx = rand_ull(0, static_cast<ull>(charset.size() - 1));
	return charset[static_cast<size_t>(idx)];
}
string rand_string(size_t length, const string& charset) {
	if (charset.empty())
		throw invalid_argument("rand_string:传入的 charset 为空!");
	if (length == 0)
		return "";
	string result;
	result.reserve(length);
	for (size_t i = 0; i < length; i++)
		result += rand_char(charset);
	return result;
}
template <typename RandomIt>
void rand_shuffle(RandomIt first, RandomIt last) {
	shuffle(first, last, get_rng());
}

ifstream fin;
ofstream fout;
void open_file(string name, string mod) {
	if(mod == "read") {
		if(fin.is_open())
			fin.close();
		fin.open(name);
		if(!fin.is_open())
			throw runtime_error("open_file(read 模式):文件 " + name + " 打开失败!");
	} else if(mod == "write") {
		if(fout.is_open()) fout.close();
		fout.open(name);
		if(!fout.is_open())
			throw runtime_error("open_file(write 模式):文件 " + name + " 打开失败!");
	} else {
		throw invalid_argument("open_file:传入的 mod 无效!应为 read/write。");
	}
}

signed main() {
	INIT();
	upl(i, 1, 10, 1) {
		open_file(FOLDER + NAME + "_" + to_string(i) + IN, "read");
		open_file(FOLDER + NAME + "_" + to_string(i) + OUT, "write");
		int a, b;
		fin >> a >> b;
		fout << a + b;
	}
	return 0;
}
原文