1.生成随机数据
背景
在出题过程中,除了一些边界点和关键数据需要自己构造,还需要构造一些正常数据,防止学生用print大法直接把答案打印出来,所以编写了一段代码示例,用于批量生成对应的in、out输入输出文件(发布到洛谷上的)。(有什么其他的方法也可以告诉我,谢谢各位大佬)
代码
(以a+b为例子)
#include <fstream>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
srand(static_cast<unsigned>(time(0)));
for (int i = 1; i <= 4; i++) {
char t = '0' + i;
ostringstream oss;
oss << "test" << i << ".in";
string s = oss.str();
// 使用 c_str() 将 string 转换为 C 风格字符串
ofstream outfile(s.c_str());
int a = 1 + rand() % 100;
int b = 1 + rand() % 100;
if (outfile.is_open()) {
outfile << a << " " << b << endl;
}
outfile.close();
ostringstream oss2;
oss2 << "test" << i << ".out";
string m = oss2.str();
ofstream outfile2(m.c_str());
if (outfile2.is_open()) {
outfile2 << a + b << endl;
}
outfile2.close();
}
return 0;
}
效果: