对拍程序 & 计时器
- 使用随机数提供数据
如下代码gen.cpp,产生 \(n\) 个 \(-1\) 和 \(1\) 的随机数,编译生成gen.exe。
#include <iostream>
#include <random>//比rand()取值范围更大,rand()最大只有32767
using namespace std;
mt19937 d1(random_device{}());
const int N = 3e4 + 7;
int a[N];
int main() {
int n = d1() % 20;
printf("%d\n", n);
for (int i = 1; i <= n; ++i) {
int x = d1()%2;
if (!x) x = -1;
printf("%d ", x);
}
return 0;
}
- 分别写出测试程序
TT.exe以及标程lec.exe,使用批处理程序对拍.bat测试,代码如下:
@echo off
:loop
gen > 1.in
TT < 1.in > 1.out
lec < 1.in > 1.ans
fc 1.out 1.ans
if not errorlevel 1 goto loop
pause
goto loop
所有程序文件均放在同一个文件夹下,然后双击 对拍.bat 开始测试。
- 对程序计时
#include <iostream>
#include <ctime>
#include <chrono>
#include <ratio>
#include <functional>
#include <algorithm>
using namespace std;
using namespace chrono;
int main() {
high_resolution_clock::time_point start = high_resolution_clock::now();
// 需要执行的代码
high_resolution_clock::time_point over = high_resolution_clock::now();
auto msec = duration_cast<microseconds>(over-start).count();
printf("%lld", msec);
}
4、自己编写编译运行代码
#!/bin/bash
g++ "$1".cpp -o "$1" -std=c++14 -O2
./"$1"

浙公网安备 33010602011771号