Algorithm exercise: Number of equivalents

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.