跳到正文

个人记录

对拍程序(A-B问题)

andycode约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年4月27日
收录
2026年1月6日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

众所周知,对拍是一个非常重要的知识点,还能帮我们造数据。

可以检测出我们代码中的问题。

所以,我就以A-B问题为例,为大家写出对拍。

我的(错误)程序(my.cpp)

看客:作者你是不是故意的?!

C++
#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b;
	cin>>a>>b;
	if(a>1000)cout<<a*2-b;
	else cout<<a-b;
	return 0;
}

暴力程序(std.cpp)

看客:好好的A-B弄那么复杂干嘛……

C++
#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b;
	cin>>a>>b;
	while(b--)a-=1;
	cout<<a;
	return 0;
}

制造随机数(data.cpp)

C++
#include <bits/stdc++.h>
using namespace std;
int main(){
	int seed=time(NULL);
	srand(seed);
	int a=rand()*rand()%1000001,b=rand()*rand()%1000001;
	cout<<a<<' '<<b;
	return 0;
}

对拍程序(dp.cpp)

C++
#include <bits/stdc++.h>
using namespace std;
int main(){
	int t,s;
	while(1){
		system("data.exe > in.txt");
		s=clock();//计算时间
		system("my.exe < in.txt > out.txt");
		t=clock();
		system("std.exe < in.txt > std.txt");
		if(system("fc out.txt std.txt"))break;//对比数据
		printf("AC time: %d\n",t-s);//输出用时
	}
	printf("WA time: %d\n",t-s);
	return 0;
}

本蒟蒻的第一篇文章,还望大家多多支持。

如有不足之处,欢迎大家指出。

注意,rand()最高是五位数

原文