《数据结构与算法之1 rand()函数》

 1 #include "stdafx.h"
 2 #include "iostream"
 3 #include "time.h"               //用到time(null)函数,需要此头文件
 4 
 5 using namespace std;
 6 
 7 int a[100000];
 8 int b[100000];
 9 
10 
11 /*******************
12 说明:系统在调用rand(),之前会自动调用srand().系统默认为1.
13 对100取余,得到的数在(0~100)之间。
14 ********************/
15 void initA(int n){
16     srand((unsigned)time(NULL));
17     for (int i=0;i<n;i++)      
18     {                            
19         a[i] = rand()%100;  //对100取余
20         b[i] = a[i];
21     }
22     return;
23 }
24 
25 
26 /************************
27 //说明:每输出10个数字,换行。(i!=0)避免,i=0的特殊情况。
28 ****************************/
29 void printA(int Num[],int n){
30     for (int i=0;i<n;i++)
31     {
32         cout<<Num[i]<<" ";
33         if ((0 == i%10)&&(i!=0))
34         {
35             cout<<endl;
36         }
37     }
38     cout<<endl;
39     return;
40 }
41 
42 /****************************
43 函数:比较函数
44 功能:
45 作者:Micheal
46 时间:2016-01-08
47 ****************************/
48 void compareNum(int Num1[],int Num2[],int n){
49     bool bCompare = true;
50     for (int i=0;i<n;i++)
51     {
52         if (Num1[i]==Num2[i])
53         {
54             bCompare = true;
55         }
56         else{
57             bCompare = false;
58         }
59     }
60     if (bCompare)
61     {
62         cout<<"the two Nums are same."<<endl;
63     } 
64     else
65     {
66         cout<<"the two Nums are not same."<<endl;
67     }
68     return;
69 
70 }
71 
72 
73 int _tmain(int argc, _TCHAR* argv[])
74 {
75     int n;
76     cin>>n;
77     initA(n);
78 
79     printA(a,n);
80     printA(b,n);
81     compareNum(a,b,n);
82 
83     printA(b,n);
84     compareNum(a,b,n);
85 
86     system("pause");
87     return 0;
88 }

 

转载于:https://www.cnblogs.com/michael2016/p/5113168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值