天天看点

C++版题目随机测试数据编写器

介绍

有许多OJ平台及各大比赛的出题人需要出很多的测试数据,但却自己编不出来?

下面这些建议你看看

(虽然网上有很多其他的bat版随机测试数据编写器,但是我还是喜欢用我这种C++版的)

主程序

.in

文件生成(

data_make_in.cpp

#include<math.h>
#include<string>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#define ll long long
using namespace std;
int R(int x){return (rand()*rand())%x+1;}  //随机数的生成
string s="name";  //在此处改为题目名(仅支持英文)
void s70(){  //对于100%的数据进行以下输出
	FILE* lin=fopen(s.c_str(),"w");
	
    
	fclose(lin);
	return ;
}
void s30(){  //对于70%的数据进行以下输出
	FILE* lin=fopen(s.c_str(),"w");
	
    
	fclose(lin);
	return ;
}
void s00(){  //对于30%的数据进行以下输出
	FILE* lin=fopen(s.c_str(),"w");
	
    
	fclose(lin);
	return ;
}
int main(){
	int tt;
	int typ=tt=20;  //在此处修改测试数据组数,默认为20
	int len=s.size();
	s+=char(tt/100+'0');
	s+=char((tt/10)%10+'0');
	s+=char(tt%10+'0');
	s+=".in";
	srand((unsigned)time(NULL));
	while(typ--){
		if(typ>=tt*70/100)  //表示对于100%的数据进行以下生成
			s70();
		else if(typ>=tt*30/100)  //表示对于70%的数据进行以下生成
			s30();
		else  //表示对于30%的数据进行以下生成
			s00();
        //如需增加其他节点可按照这些样例来加入
		s[len]='0';
		s[len+1]='0'+typ/10;
		s[len+2]='0'+typ%10;
	}
	return 0;
}
           

在上方代码的标记位置修改测试数据组数以及题目名(仅支持英文)。

在上方代码留空的地方进行生成操作。注意 :输出需用

fprintf(lin,项目1,项目2)

,详情可自行搜索

.out

文件生成(

data_make_out.cpp

#include<cstdlib>
#include<iostream>
#include<string>
#include<cstring>
#include<ctime>
#include<cstdio>
#include<windows.h>
using namespace std;
int main(){
	string name,in=".in",out=".out",exe=".exe",exefile=".exe",infile=".in",outfile=".out";
	int n;
	printf("请输入题目名:");
	cin>>name;
	printf("请输入数据in个数:");
	scanf("%d",&n);
	exefile.insert(0,name);
	infile.insert(0,name);
	outfile.insert(0,name);
	string cmd2="del ",cmd4="del ";
	for(int i=n;i>=1;i--){
		string cmd1="copy ",cmd3="copy ";
		cmd1+=name;
		cmd1+=char(i/100+'0');
		cmd1+=char((i/10)%10+'0');
		cmd1+=char(i%10+'0');
		cmd1+=in;
		cmd1+=" ";
		cmd1+=infile;
		system(cmd1.c_str());
		system(exefile.c_str());
		cmd3+=outfile;
		cmd3+=" ";
		cmd3+=name;
		cmd3+=char(i/100+'0');
		cmd3+=char((i/10)%10+'0');
		cmd3+=char(i%10+'0');
		cmd3+=out;
		system(cmd3.c_str());
		printf("NO.%d\n",i); 
	}
	cmd2+=infile;
	cmd4+=outfile;
	system(cmd2.c_str());
	system(cmd4.c_str());
	return 0;
}
           

上面的代码你不需要进行任何的改动,直接复制然后编译运行即可,照着运行要求进行就行

注意 :所有前面生成的所有

.in

文件、正解的

.exe

文件和上面这个

.cpp

编译出来的

.exe

文件都要放在同一目录下!!

注意 :正解需要用加上

freopen

,输入文件名为

题目名+.in

,输出文件名为

题目名+.out

,更多有关内容可自行搜索

结果

结尾

c++

继续阅读