zoukankan      html  css  js  c++  java
  • 《数据结构与算法之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 }
  • 相关阅读:
    TOYS POJ
    口罩发放
    Qin Shi Huang's National Road System HDU
    次小生成树
    ACM Contest and Blackout UVA
    Strongly connected HDU
    tarjan
    Network HDU
    【洛谷 1351】联合权值
    【洛谷 3884】二叉树问题
  • 原文地址:https://www.cnblogs.com/michael2016/p/5113168.html
Copyright © 2011-2022 走看看