The intersection of two sorted arrays

后端 未结 8 1423
臣服心动
臣服心动 2020-11-29 02:21

Given two sorted arrays: A and B. The size of array A is La and the size of array B is Lb. How

相关标签:
8条回答
  • 2020-11-29 03:14
    void Intersect()
    {
        int la, lb;
        la = 5;
        lb = 100;
        int A[5];
        int i, j, k;
        i = j = k = 0;
        for (; i < 5; ++i)
            A[i] = i + 1;
        int B[100];
        for (; j < 100; ++j)
            B[j] = j + 2;
        int newSize = la < lb ? la : lb;
        int* C = new int[newSize];
        i = j = 0;
        for (; k < lb && i < la && j < lb; ++k)
        {
            if (A[i] < B[j])
                i++;
            else if (A[i] > B[j])
                j++;
            else
            {
                C[k] = A[i];
                i++;
                j++;
            }
        }
        for (k = 0; k < newSize; ++k)
            cout << C[k] << NEWLINE;
    }
    
    0 讨论(0)
  • 2020-11-29 03:16
     //intersection of two arrays
    #include<iostream>
    using namespace std;
    int main() {
    
    int i=0,j=0,m,n;
    int arr1[i],arr2[j];
    cout<<"Enter the number of elements in array 1: ";
    cin>> m;
    cout<<"Enter the number of elements in array 2: ";
    cin>>n;
    for (i=0;i<m;i++){
        cin>> arr1[i];
    }
    for(j=0;j<n;j++){
        cin>> arr2[j];
    }
    for(j=0;j<n;j++){
        for(i=0;i<m;i++) {
            if (arr1[i] == arr2[j]){
            cout<< arr1[i];
            cout << ' ';
            break;
            }
        } 
     }    
    
     return 0;
     }
    
    0 讨论(0)
提交回复
热议问题