对拍就是指解题程序与暴力程序之间的输出对比。
以计算2的幂为例:
输入:5
\qquad
输出32
一、编写解题程序:“std.cpp”
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cin>>n;
cout<<pow(2,n); //pow函数:pow(n,m):n的m次方
return 0;
}
二、编写生成随机数的程序:“rand.cpp”
#include<bits/stdc++.h>
using namespace std;
int main(){
srand(time(NULL));
int n=rand()%10000+1; //生成一个随机数,其区间在1~10000之间。
cout<<n;
return 0;
}
这样出来的数是随机的,可以用来判断程序的正确性。
三、编写暴力程序:“baoli.cpp”
#include<bits/stdc++.h>
using namespace std;
int n;
int ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++) ans*=2;
cout<<ans;
return 0;
}
暴力程序虽然好想,但可能会超时(这个肯定不会,我只是举个例子),所以只能用来参考。
四、编写对拍程序:
生成的随机数会当成暴力程序和解题程序的输入数据。
@echo off
:loop
rand.exe>data.in
std.exe>data.in>std.out
baoli.exe<data.in>baoli.out
fc baoli.out std.out
if not errorlevel 1 goto loop
pause
goto loop
此程序在记事本中编辑,保存成txt文件后,直接将其后缀名改成bat,然后直接运行即可。