【算法题】找出两个已序数组,是否含有相同的数字

问题描述:

   找出两个已序数组,是否含有相同数字。

代码解决:

 1 /*
 2     version  1.0
 3     Created  16:15 2011-6-29
 4     Author   fan
 5  */
 6 #include <iostream>
 7 using namespace std;
 8 
 9 bool SameNumber(int arrA[],int arrB[],int lengthA,int lengthB)
10 {
11     int i,j;
12     i=0;
13     j=0;
14     while(i<lengthA &&j<lengthB)
15     {
16         if (arrA[i]<arrB[j])  i++;
17         else 
18             if (arrA[i]>arrB[j]) j++;
19         else 
20             return true;
21     }
22     return false;
23 }
24 
25 void InputArray(int arrX[],int length)
26 {
27     int i;
28     for (i=0;i<length;i++)
29     {
30         cin>>arrX[i];
31     }
32 }
33 
34 int main()
35 {
36      int arrA[10];
37      int arrB[10];
38      int lengthA;
39      int lengthB;
40      
41      cin>>lengthA;
42      InputArray(arrA,lengthA);
43      cin>>lengthB;
44      InputArray(arrB,lengthB);
45      
46      if (SameNumber(arrA,arrB,lengthA,lengthB))
47      {
48          cout<<"the two Array have the same number!"<<endl;
49      }
50      else
51      {
52          cout<<"the two Array don't have the same number!"<<endl;
53      }
54      
55      return 0;
56 }
原文地址:https://www.cnblogs.com/paulbai/p/3149507.html