luogu 数据生成

luogu 数据生成利器

test

test.exe 是指标准程序,不能出错。一般把 gentest 放在一起。

示例 test

#include <bits/stdc++.h>
using namespace std;

int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

数据生成

先贴代码:

#include <bits/stdc++.h>
using namespace std;

void printf_green(const char *s)
{
	printf("\033[0m\033[1;32m%s\033[0m\n", s);
}

int main(){
	
	for(int i = 1;i <= 100;i++){// 数据范围 
		
		string str = to_string(i);
		while(str.size() < 3){
			str = "0" + str;
		}
		string aa = "gen > " + str + ".in.txt";
		string bb = "test.exe < " + str +".in.txt > " + str + ".out.txt";
		string ct = "OK on test " + str;
		const char* cc = nullptr;
		const char* dd = nullptr;
		const char* ct1 = nullptr;
		cc = aa.c_str(), dd = bb.c_str(), ct1 = ct.c_str();
		system(cc);
		system(dd);
		printf_green(ct1);
	}
	
	return 0;
}

其中,第六行的 for 循环,表示在同一个 gen 的情况下数据生成个数。

\(9\) 行的 while 循环,意在把数字补前导 \(0\)

随机图生成

随机图生成

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int g[N][N];

int main(){
	
	srand(time(0));
	int n = 10, m = 30;// 表示结点个数和边个数
	int relm = 0;
	
	for(int i = 2;i <= n;i++){
		int deg = m/n;// 表示每一条边的出边
		for(int v = 1;v <= deg;v++){
//			cout<<"come\n";
			int u = rand() % (i-1) + 1;
			if(!g[u][i])	relm++;
			g[u][i] = 1;// 可以加权 
			g[i][u] = 1;
		}
	}
	cout<<n<<" "<<relm<<"\n";
	for(int i = 1;i <= n;i++){
		for(int j = i;j <= n;j++){
			if(g[i][j])	cout<<i<<" "<<j<<"\n";// 去重输出 
		}
	}
	return 0;
} 

很明显,如果要这个图中的边不完全是 \(m\),所以需要使用 \(relm\) 这个变量存新 \(m\)

随机非简单图生成

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int g[N][N];

struct node{
	int u, v;
};

queue<node> q;

int main(){
	
	srand(time(0));
	
	int n = 10, m = 100;// 表示结点个数和边个数
	for(int i = 2;i <= n;i++){
		int deg = m/n;// 表示每一条边的出边
		for(int v = 1;v <= deg;v++){
			int u = rand() % i + 1;
			q.push({v, i});
		}
	}
	cout<<n<<" "<<q.size()<<"\n";
	while(!q.empty()){
		cout<<q.front().u<<" "<<q.front().v<<"\n";
		q.pop();
	}
	return 0;
}

随机树生成

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int g[N][N];

int main(){
	
	srand(time(0));
	int n = 100;// 表示结点个数
	
	for(int i = 2;i <= n;i++){
		int u = rand() % (i-1) + 1;
		g[i][u] = 1;// 可以加权 
		g[u][i] = 1;
	}
	cout<<n<<"\n";
	for(int i = 1;i <= n;i++){
		for(int j = i;j <= n;j++){
			if(g[i][j])	cout<<i<<" "<<j<<"\n";// 去重输出 
		}
	}
	return 0;
} 

就非常简单易懂。对于每个节点,只连接随机的父亲,就可以做到随机树了。

Masonの对拍

朴素考场对拍

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main() {
	for (int i = 1;i <= 100;i++) {
		system("gen > test.in.txt");
		system("test1.exe < test.in.txt > a.out.txt");
		system("test2.exe < test.in.txt > b.out.txt");
		std::cout<<i<<"small\n";
		if (system("fc a.out.txt b.out.txt")) {
			system("pause");
			return 0;
		}
	}
}

小数据+大数据对拍

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main() {
	for (int i = 1;i <= 100;i++) {
		system("gen1 > test.in.txt");
		system("test1.exe < test.in.txt > a.out.txt");
		system("test2.exe < test.in.txt > b.out.txt");
		std::cout<<i<<"small\n";
		if (system("fc a.out.txt b.out.txt")) {
			system("pause");
			return 0;
		}
	}
	for (int i = 1;i <= 50;i++) {
		system("gen2 > test.in.txt");
		system("test1.exe < test.in.txt > a.out.txt");
		system("test2.exe < test.in.txt > b.out.txt");
		std::cout<<i<<"big\n";
		if (system("fc a.out.txt b.out.txt")) {
			system("pause");
			return 0;
		}
	}
}

BEAUTIFUL の 对拍

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

void printf_red(const char *s)
{
    printf("\033[0m\033[1;31m%s\033[0m", s);
}

bool check()
{
	string ans,ans2;
	freopen("a.out.txt","r",stdin);
	char c;
	while(scanf("%c",&c)!=EOF) ans+=c;
	fclose(stdin);
	freopen("b.out.txt","r",stdin);
	while(scanf("%c",&c)!=EOF) ans2+=c;;
	fclose(stdin);
	return ans == ans2;
}
int main() {
	for(int i = 1; i <= 500;i++) {
		system("gen > test.in.txt");
		system("test1.exe < test.in.txt > a.out.txt");
		system("test2.exe < test.in.txt > b.out.txt"); 
		
		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{
			printf_red("WA on test ");
			string a = to_string(i);
			char ch[1000];
			strcpy(ch, a.c_str());
			printf_red(ch);
			cout<<"\n";
			return 0;
		}
	}
	printf_green("ALL TESTS RIGHT");
}
posted @ 2024-08-12 20:20  Mason123456  阅读(127)  评论(0)    收藏  举报