Content: Known two integer arrays f[] and g[], their elements have been arranged from small to large, and the elements in each of the two arrays are different. For example, there are 1,3,4,7,9 in f[], and 3,5,7,8,10 in g[]. The trial program calculates how many groups of the same elements are in between the two arrays. In the case of the above example, f[2] and G[1] is a group of 3, F[4] and G[3] 8 is a group. So there are two groups.
After the first two days of writing, I think the program is not representative, so I intend to later the core algorithm to write out the form of the function, so that looks more intuitive.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
My solution: Come up didn't think much, open the vs2013 to knock up, the problem is really very simple, minutes on the super God. Oh, no, it's settled! After the inspiration of yesterday's topic, I took into account the problem of time complexity, so today directly reached the level of N, indicating that I did progress O (∩_∩) o~!
#include <iostream>
using namespace std;
int _tmain (int argc, _tchar* argv[])
{
int equals (int f[], int g[],int flength,int);
int Num = 0;
int f[5] = {1, 3, 4, 7, 9};
int g[5] = {3, 5, 7, 8,};
Num = equals (f,g,5,5);
cout << "The number of equivalents in two arrays is:";
cout << Num << Endl;
GetChar ();
return 0;
}
int equals (int f[], int g[],int flength,int glength)
{
int Num = 0;
int index_f = 0;
int index_g = 0;
while (Index_f < flength && Index_g < glength)
{
if (f[index_f] = = G[index_g])
{
num++ ;
index_f++;
}
else if (F[index_f] < G[index_g])
index_f++;
else
index_g++;
}
return Num;
}
The experimental results are: