c_cpp 在三个排序的数组中查找公共元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 在三个排序的数组中查找公共元素相关的知识,希望对你有一定的参考价值。

// https://www.geeksforgeeks.org/find-common-elements-three-sorted-arrays/
#include <iostream>
using namespace std;

int main(){
    int n,m,p;
    cout<< "Enter the no.of elements:";
    cin>>n>>m>>p;

    int a[n], b[m], c[p];
    cout<< "Enter the first array elements:";
    for (int i=0;i<n;i++)
        cin>> a[i];
    cout<< "Enter the second array elements:";
    for (int i=0;i<m;i++)
        cin>> b[i];
    cout<< "Enter the third array elements:";
    for (int i=0;i<p;i++)
        cin>> c[i];

    int i=0,j=0,k=0;
    while (i<n && j<m && k<p){
         if (a[i] == b[j] && b[j] == c[k]){
            cout << a[i] << "  ";
            i++;
            j++;
            k++;
         }
         else if (a[i] < b[j])
            i++;
         else if (b[j] < c[k])
            j++;
         else
            k++;
    }
}

以上是关于c_cpp 在三个排序的数组中查找公共元素的主要内容,如果未能解决你的问题,请参考以下文章