简单的数据生成方法

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

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

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

  后面比如要想产生n个数,可以先int n=多少,再printf("%d\n",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", n);
18         for (int i = 1; i <= n; i++) {
19             int t = rand() % 10000 + 1;
20             printf("%d ", t);
21         }
22         printf("\n");
23     }
24     return 0;
25 }

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

转载于:https://www.cnblogs.com/zxhyxiao/p/7419697.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值