跳到正文

个人记录

对拍

G1yu约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年3月2日
收录
2026年7月25日
更新
2026年11月12日
篇幅
约 5 分钟

版本

  1. v1当前

对拍

可以选择对拍次数的对拍程序( 00 表示执行 ∞\infty 次,其他数 n(0≤n)n (0\leq n) 表示执行nn次)
这是我的对拍代码,请参考:

C++
#include <stdio.h>
#include<iostream>
#include <stdlib.h>
using namespace std;
int main() {
	int n;
	cin>>n;
	for(int i = 1; n?i<=n:1;i++) {
		system("gen > test.in");  
		system("sol.exe < test.in > a.out"); //sol:要对的程序
		system("ans.exe < test.in > b.out");  //ans:保证对的暴力代码
		cout<<i<<endl;
		if (system("fc a.out b.out")) {
			system("pause");  
			return 0;
		}
	}
	cout<<"AC";
}

v2

C++
#include <bits/stdc++.h>
using namespace std;
void printf_green(const char *s)
{
	printf("\033[0m\033[1;32m%s\033[0m", s);
}

bool check()
{
	string ans,ans2;
	freopen("a.out","r",stdin);
	char c;
	while(scanf("%c",&c)!=EOF) ans+=c;
	fclose(stdin);
	freopen("b.out","r",stdin);
	while(scanf("%c",&c)!=EOF) ans2+=c;;
	fclose(stdin);
	return ans == ans2;
}
int main() {
	int n;
	cin>>n;
	for(int i = 1; n?i<=n:1;i++) {
		system("gen > test.in");
		system("sol.exe < test.in > a.out");
		system("ans.exe < test.in > b.out"); 
		
		if (check()) {
			printf_green("AC on test ");
			string a = to_string(i);
			char ch[1000];
			strcpy(ch,a.c_str());
			printf_green(ch);
			cout<<"\n";
		}
		else{
			cout<<"WA!!!!"<<endl;
			return 0;
		}
	}
	printf_green("ALL TESTS RIGHT");
}

V3

C++
#include <bits/stdc++.h>
#include<windows.h>
using namespace std;
void printf_green(const char *s)
{
	printf("\033[0m\033[1;32m%s\033[0m", s);
}
BOOL SetConsoleColor(WORD wAttributes)  
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  
	if (hConsole == INVALID_HANDLE_VALUE)  
		return FALSE;  
	
	return SetConsoleTextAttribute(hConsole, wAttributes);  
}  
bool check()
{
	string ans,ans2;
	freopen("a.out","r",stdin);
	char c;
	while(scanf("%c",&c)!=EOF) ans+=c;
	fclose(stdin);
	freopen("b.out","r",stdin);
	while(scanf("%c",&c)!=EOF) ans2+=c;;
	fclose(stdin);
	return ans == ans2;
}
int main() {
	int n;
	cin>>n;
	for(int i = 1; n?i<=n:1;i++) {
		system("gen > test.in");
		system("sol.exe < test.in > a.out");
		system("ans.exe < test.in > b.out"); 
		
		if (check()) {
				SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_GREEN);
				printf("[AC in %d]",i);
				cout<<"\n";
		}
		else{
			SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED);
			printf("[WA in %d]",i);
			
			return 0;
		}
	}
	printf_green("ALL TESTS RIGHT");
}
原文
存档评论1 条抓取于