对拍程序 & 计时器

  1. 使用随机数提供数据
    如下代码 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;
}
  1. 分别写出测试程序 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 开始测试。

  1. 对程序计时
#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"
posted @ 2024-04-14 11:44  飞花阁  阅读(28)  评论(0)    收藏  举报
//雪花飘落效果