用c++实现求两个数组的交集
时间: 2023-09-04 07:16:14 浏览: 121
```c
#include <stdio.h>
void intersection(int array1[], int size1, int array2[], int size2);
int main() {
int array1[] = {1, 2, 3, 4, 5};
int array2[] = {3, 4, 5, 6, 7};
int size1 = sizeof(array1) / sizeof(array1[0]);
int size2 = sizeof(array2) / sizeof(array2[0]);
intersection(array1, size1, array2, size2);
return 0;
}
void intersection(int array1[], int size1, int array2[], int size2) {
int i, j;
printf("Intersection of the two arrays: ");
for (i = 0; i < size1; i++) {
for (j = 0; j < size2; j++) {
if (array1[i] == array2[j]) {
printf("%d ", array1[i]);
break;
}
}
}
}
```
这个程序会输出:Intersection of the two arrays: 3 4 5。
阅读全文