Top

对数器模板


博主CSDN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//需要建立主路径 和主路径下sample/ 保证程序文件存在
#include <bits/stdc++.h>
using namespace std;

string mpat("D:/dsq/"); //主路径 注意末尾跟'/'!!!! linuxpwd查看当前路径
string an("A"), bn("B"); //程序名字
int brek = 5; //几个错误停止 -1不停止
int cmpt = 1; //编译模式

void exec(const string &cmd, string &res)
{
char buf[1024];
FILE *fp = _popen(cmd.c_str(), "r"); //linux下改为popen
while (fgets(buf, 1024, fp))
res.append(buf);
_pclose(fp); //linux下改为pclose
}
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 main()
{
srand(time(NULL));
rand();
if (cmpt) //编译功能
{
system(("g++ " + mpat + an + ".cpp -o " + mpat + an + ".exe -std=c++11").c_str()); //C++11支持
system(("g++ " + mpat + bn + ".cpp -o " + mpat + bn + ".exe -std=c++11").c_str());
}
string num, aout, bout, spat = mpat + "sample/"; //样例生成子路径
stringstream ss;
int cnt = 0, wa = 0;
for (int i = 1; i <= brek; i++)
{
ss.clear(), ss << i, ss >> num;
remove((spat + num + "samp.txt").c_str());
remove((spat + num + "ans" + an + ".txt").c_str());
remove((spat + num + "ans" + bn + ".txt").c_str());
}
while (wa < brek && cnt < 1e4) //几个错误停止 极限1W个样例
{
ss.str(""), ss.clear(), ss << wa + 1, 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)
{
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;
}

文章版权为Anoyer博客所有,转载请以链接形式标明本文地址