数据生成器

· · 个人记录

剪切板:[https://www.luogu.com.cn/paste/smn9kljc](https://www.luogu.com.cn/paste/smn9kljc) 使用教程: ```cpp char randChar(char begin, char end); 返回一个取值范围为[begin,end]的字符。 template <typename T> T randNum(T begin, T end); 返回一个取值范围为[begin,end]的数字。 char* randString(const int& length, const bool& capitalLetterChar, const bool& littleLetterChar, const bool& numberChar) length为字符串的长度,其余的意为“字符串中是否包含某种类型的字符”,例如,numberChar为true则字符串中包含数字字符。 返回一个不包含空格和换行的字符串。 ``` 示例程序: ```cpp #include <ctime> #include "random" //注意自己写的头文件用双引号""括起来 #include <cstdio> #include <iostream> using namespace std; int main() { //random中已包含cstdlib头文件 srand(time(NULL)); //生成一个随机字符,取值范围为[begin, end] printf("%c\n", randChar('0', '9')); //生成一个随机数字,取值为[begin, end] printf("%d\n", randNum(10000, 200000)); //生成一个随机字符串 //长度为100,包含大写字母,小写字母和数字 printf("%s\n", randString(100, 1, 1, 1)); return 0; } ``` 头文件```random```源代码如下: ```cpp #include <cstdlib> #include <cstring> char randChar(char, char); template <typename T> T randNum(T, T); char* randString(const int&, const bool&, const bool&, const bool&); char randChar(char begin, char end) { char ch; ch = (char)(rand() % (end - begin + 1) + begin); return ch; } template <typename T> T randNum(T begin, T end) { T ans; ans = (T)(((double)rand() * 1.0 / RAND_MAX) * (double)(end - begin) + begin); return ans; } char* randString(const int& length, const bool& capitalLetterChar, const bool& littleLetterChar, const bool& numberChar) { char* ans = new char[length + 5]; memset(ans, 0, sizeof(char) * (length + 5)); int cnt = 0; for ( ; cnt < length; ) { int a = rand() % 3; if (a == 0 && capitalLetterChar) ans[cnt++] = randChar('A', 'Z'); else if (a == 1 && littleLetterChar) ans[cnt++] = randChar('a', 'z'); else if (a == 2 && numberChar) ans[cnt++] = randChar('0', '9'); } return ans; } ``` 目前只有这三个函数,大家可以帮忙拓展。 建议:生成较大整数可以使用随机字符串,```randNum```可以生成浮点数。 注意:自己写的头文件要用```""```括起来。 对拍实例: [P1914 小书童——密码](https://www.luogu.com.cn/problem/P1914) 大佬写的正解储存在```debug.cpp```中,我写的储存在```main.cpp```中 ```cpp /*main.cpp*/ #include <cstdio> #include <cstring> char a[55]; int main() { int n; scanf("%d\r",&n); gets(a); for (int i=0;a[i]!='\0';i++) { if (a[i]+n>='a'&&a[i]+n<='z') { a[i]+=n; } else { a[i]-=26%n; } } printf("%s",a); return 0; } ``` ```cpp /*debug.cpp*/ #include <string> #include <iostream> #include <cstdio> using namespace std; int main() { string s; int n; scanf("%d", &n); cin >> s; for (int i = s.length() - 1; i >= 0; --i) s[i] = ((s[i] - 'a') + n) % 26 + 'a'; cout << s << endl; return 0; } ``` 接下来就可以使用```random```了。 ```cpp /*maker.cpp*/ #include "random" #include <ctime> #include <cstdio> int main() { srand(time(NULL)); printf("%d\n", randNum(1, 10000)); printf("%s\n", randString(randNum(1, 50), false, true, false)); return 0; } ``` 对拍程序就可以写出来了($Windows$系统下): ```cpp /*test.cpp*/ #include <cstdio> #include <iostream> using namespace std; int main() { int n, kase = 0; puts("Input the number of the test cases:"); scanf("%d", &n); //做n次测试 for (int i = 0; i < n; ++i) { system("maker.exe>inp"); system("main<inp>out1"); system("debug<inp>out2"); //如果两个测试文件相同 if (!system("fc out1 out2 > nul")) { printf("The %d case is OK.\n", ++kase); } else { //找到错误 puts("Find error!"); puts("inp"); system("type inp"); puts("out1"); system("type out1"); puts("out2"); system("type out2"); return 0; } } puts("All the cases are OK."); return 0; } ```