用途:
- 用于比赛的时候程序产生错误,却苦于无法找到错误的数据样例。
使用方式
- 首先
修改
对数器里面的mpat("C:/Users/");
为当前文件所在位置。
- 编辑
sample(stringstream &sout)
可以生成样例。(建议先生成小范围数据)
- 当出现错误样例数超过
5次
,则会停止对拍。
- 可以在
sample
文件夹下查看生成的错误样例。
对数器代码
#include <bits/stdc++.h>
using namespace std;
string mpat("C:/Users/");
string an("A"), bn("B");
int brek = 5;
int cmpt = 1;
void exec(const string &cmd, string &res)
{
char buf[1024];
FILE *fp = _popen(cmd.c_str(), "r");
while (fgets(buf, 1024, fp))
res.append(buf);
_pclose(fp);
}
void wrfile(const string &path, stringstream &ss)
{
string str;
FILE *fp = fopen(path.c_str(), "wb");
while (getline(ss, str))
fprintf(fp, "%s\r\n", str.c_str());
fclose(fp);
}
long long d_rand(long long l, long long r)
{
int k = rand() % 4;
long long t = rand();
for (int i = 0; i < k; i++)
t = t << 16 | rand();
return l + t % (r - l + 1);
}
double f_rand(double l, double r)
{
double t = rand() / 32767.0;
return l + t * (r - l);
}
char c_rand(const string &ch)
{
return ch[rand() % ch.size()];
}
void sample(stringstream &sout)
{
int n=d_rand(1,5);
printf("%d\n",n);
}
int main()
{
srand(time(NULL));
rand();
if (cmpt)
{
system(("g++ " + mpat + an + ".cpp -o " + mpat + an + ".exe -std=c++11").c_str());
system(("g++ " + mpat + bn + ".cpp -o " + mpat + bn + ".exe -std=c++11").c_str());
}
string num, aout, bout, spat = mpat + "sample/";
int cnt = 0, wa = 0;
while (wa != brek)
{
stringstream ss;
ss << ++cnt;
ss >> num;
aout.clear(), bout.clear(), ss.clear();
sample(ss);
wrfile(spat + num + "samp.txt", ss);
cout << cnt;
exec(mpat + an + ".exe < " + spat + num + "samp.txt", aout);
exec(mpat + bn + ".exe < " + spat + num + "samp.txt", bout);
if (aout == bout)
{
remove((spat + num + "samp.txt").c_str());
remove((spat + num + an + "ans" + an + ".txt").c_str());
remove((spat + num + bn + "ans" + bn + ".txt").c_str());
}
else
{
ss.clear();
ss.str(aout);
wrfile(spat + num + "ans" + an + ".txt", ss);
ss.clear();
ss.str(bout);
wrfile(spat + num + "ans" + bn + ".txt", ss);
wa++;
}
cout << "\t" << wa << endl;
}
return 0;
}