我正在用c ++进行压力测试,无法读取文件。我使用两个文件“ input.txt”(用于stdin)和“ output.txt”(用于stdout)。有两种解决方案,第二种是正确但缓慢的解决方案,第一种是快速但错误的解决方案。
...
void normalIO() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
void reverseIO() {
freopen("output.txt", "r", stdin);
freopen("input.txt", "w", stdout);
}
void solve() {
normalIO();
int a, b;
cin >> a >> b; /// This is ok
cout << a + b;
}
void slowButRightSolve() {
normalIO();
int a, b;
cin >> a >> b; /// This is garbage
for (int i = 0; i < b; ++i) {
++a;
}
cout << a;
}
int getAnswer() {
reverseIO();
int x;
cin >> x;
return x;
}
void stressTest() {
int a = ...;
int b = ...;
reverseIO();
cout << a << ' ' << b << endl;
solve();
int answer1 = getAnswer(); /// right answer
reverseIO();
cout << a << ' ' << b << endl;
slowButRightSolve();
int answer2 = getAnswer(); /// garbage
assert(answer1 == answer2);
}
...
查看字幕。第一个测试是正确的,但第二个测试是错误的。我不知道为什么对不起,我的英语不好。