C++ 合并排序算法在一个条件下合并第三个数组中的两个数组
这是一种在一定条件下合并两个数组的算法。它首先比较两个数组的索引,如果arrayP1的索引等于arrayp2的索引,它会将该索引存储到应答数组中。 有人能帮我把这个算法翻译成c语言吗++C++ 合并排序算法在一个条件下合并第三个数组中的两个数组,c++,C++,这是一种在一定条件下合并两个数组的算法。它首先比较两个数组的索引,如果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
#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;
}
也许有人可以。我也这么认为,看起来没那么难。到目前为止你试过什么?包括不合并但与2个排序集相交的代码。您的标题有误导性,因为它不是合并两个数组的算法。这是一个求交的算法。请将您的问题显示出来。您应该至少包括一个大纲,但最好是一个有问题的代码的大纲,然后我们可以尝试帮助解决具体问题。你也应该阅读。
#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];
}
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;
}