输出两个数组中的不同元素
程序员文章站
2024-01-06 13:41:34
...
#include
#include<stdlib.h>
using namespace std;
void Find_different(int* arr1, int* arr2,int len1,int len2)
{
for (int i = 0;i < len1;i++)
{
for (int j = 0;j < len2;j++)
{
if (arr1[i] == arr2[j])
{
break;
}
if (j == len2 - 1)
{
cout << arr1[i]<<" ";
}
}
}
for (int i = 0;i < len2;i++)
{
for (int j = 0;j < len1;j++)
{
if (arr2[i] == arr1[j])
{
break;
}
if (j == len1 - 1)
{
cout << arr2[i]<<" ";
}
}
}
}
int main()
{
int arr1[100] = { 0 };
int arr2[100] = { 0 };
cout << "please input the length of arr1: ";
int len1 = 0;
cin >> len1;
cout << "please input the ar1’s global: ";
for (int i = 0;i < len1;i++)
{
cin >> arr1[i];
}
cout << "please input the length of the arr2: ";
int len2 = 0;
cin >> len2;
cout << "please input the arr2’s global: ";
for (int i = 0;i < len2;i++)
{
cin >> arr2[i];
}
Find_different(arr1, arr2,len1,len2);
return 0;
}