-4
這是一種在特定條件下合併兩個數組的算法。它首先比較兩個數組的索引,並且如果arrayP1的索引等於arrayp2的索引,則它將該特定索引存儲到答案數組中。 任何一個可以幫助該算法在C++翻譯合併排序算法合併兩個數組中的第三個條件
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
int sizeofarray=0,i=0 ,j=0, num=0, answers[]={};
cout<<"enter the size of array"<<endl;
cin>>sizeofarray;//take size of array from user
int array1[sizeofarray];
int array2[sizeofarray];
cout<<"please enter a sorted array member of Array1"<<endl;
//input of array element
for (i=0 ; i<=sizeofarray; i++)
{
cin>>array1[i];
}
system("CLS");
cout<<"please enter a sorted array member of Array2"<<endl;
for (j=0 ; j<=sizeofarray; j++)
{
cin>>array2[j];
}
***strong text***system("CLS");
//comparing the array element and storing the similar items to another array
while(array1[i]!=NULL && array2[j]!=NULL){
if(array1[i]==array2[j]){
answer[num++]=array1[i];
i++;
j++;
}
else if(array1[i]<array2[j])
{
i++;
}else{
j++;
}
i++;
j++;
}
cout<<"The number of common elements"<<num<<endl;
cout<<"These are the common numbers: ";
for (int k=0;k<num;k++){
cout<<answer[k]<<" ";
}
getch();
return 0;
}
有人也許可以。 –
我也這麼認爲,看起來不那麼難。你試過什麼了?包含您的代碼 – dmaij
這不是合併,而是相交2個有序集合。你的標題是誤導性的。 – knivil