简单的数据生成方法

  添加头文件<time.h>,<stdlib.h>。

  数据生成器写:freopen("X:\x.txt","w",stdout);

  再写:srand(time(NULL))用时间作为产生随机生成数的种子。

  后面比如要想产生n个数,可以先int n=多少,再printf("%d ",n),再for(int i=1;i<=n;i++){int a,a=rand()%1000+1,printf(" %d");}//产生的数a小于1000

  

  程序主函数里写:

    freopen("X:\x.txt","r",stdin);

    freopen("X:\y.txt","w",stdout);

举个栗子:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<ctime>
 4 #include<cstdlib>
 5 #include<cstring>
 6 using namespace std;
 7 int a[105];
 8 
 9 int main()
10 {
11     freopen("F:\1.txt", "w", stdout);
12     srand(time(NULL));
13     int n;
14     for (int cas = 1; cas <= 30; cas++)
15     {
16         n = rand() % 100 + 1;
17         printf("%d
", n);
18         for (int i = 1; i <= n; i++) {
19             int t = rand() % 10000 + 1;
20             printf("%d ", t);
21         }
22         printf("
");
23     }
24     return 0;
25 }

命令行下可用 fc 1.txt 2.txt判断两个文件是否相同。

原文地址:https://www.cnblogs.com/zxhyxiao/p/7419697.html