随机数据生成

模板:

#include <cstdlib>
#include <ctime>

int random(int n) {
	return (long long)rand() * rand() % n;
}

int main() {
	srand((unsigned)time(0)); 
}

随机生成整数序列:

#include <bits/stdc++.h>

using namespace std; 

int random(int n) {
	return (long long)rand() * rand() % n;
}

const int N = 1e5, M = 1e9; 
int a[N + 100]; 

int main() {
	srand((unsigned)time(0)); 
	int n = random(N) + 1; 
	for (int i = 1; i <= n; ++i) {
		a[i] = random(2 * M + 1) - M; 
		printf("%d ", a[i]); 
	}
}

随机生成区间列:

#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>

using namespace std; 

int random(int n) {
	return (long long)rand() * rand() % n;
}

int m = 1e5, n = 1e9; 

int main() {
	freopen("czx.in", "w", stdout); 
	srand((unsigned)time(0)); 
	for (int i = 1; i <= m; ++i) {
		int l = random(n) + 1, r = random(n) + 1; 
		if (l > r) swap(l, r); 
		printf("%d %d\n", l, r);  
	}
}

随机生成树:

#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>

using namespace std; 

int random(int n) {
	return (long long)rand() * rand() % n;
}

const int N = 1e5, M = 1e9; 

int main() {
	freopen("czx.in", "w", stdout); 
	srand((unsigned)time(0)); 
	int n = random(N) + 2; 
	for (int i = 2; i <= n; ++i) {
		int fa = random(i - 1) + 1; 
		int val = random(M) + 1; 
		printf("%d %d %d\n", fa, i, val); 
	}
}

随机生成图:

#include <bits/stdc++.h>

using namespace std; 

inline void print(int x) {
	if (x < 0) putchar('-'), x = -x; 
	if (x < 10) putchar(x + '0'); 
	else {
		print(x / 10); 
		putchar(x % 10 + '0'); 
	}
}

int random(int n) {
	return (long long)rand() * rand() % n;
}

const int M = 1e6; 

int n = 1e5, m = 1e6; 
pair<int, int> e[M + 100]; 
map< pair<int, int>, bool > h; 

int main() {
	freopen("czx.in", "w", stdout); 
	srand((unsigned)time(0)); 
	print(n); putchar(' '); print(m); putchar('\n');
	for (int i = 1; i < n; ++i) {
		int fa = random(i) + 1;
		e[i] = make_pair(fa, i + 1); 
		h[e[i]] = h[make_pair(i + 1, fa)] = 1; 
	}
	for (int i = n; i <= m; ++i) {
		int x, y; 
		do {
			x = random(n) + 1, y = random(n) + 1; 
		} while (x == y || h[make_pair(x, y)]); 
		e[i] = make_pair(x, y); 
		h[e[i]] = h[make_pair(y, x)] = 1; 
	}
	random_shuffle(e + 1, e + m + 1); 
	for (int i = 1; i <= m; ++i) {
		print(e[i].first); putchar(' '); print(e[i].second); putchar('\n');
	} 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值