c语言求数组差集的程序
时间: 2023-11-24 11:51:51 浏览: 139
python获得两个数组交集、并集、差集的方法
5星 · 资源好评率100%
以下是一个用C语言实现求两个数组差集的程序:
```c
#include <stdio.h>
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {3, 4, 5, 6, 7};
int len1 = sizeof(arr1) / sizeof(arr1[0]);
int len2 = sizeof(arr2) / sizeof(arr2[0]);
int i, j, k, flag;
printf("Array1: ");
for (i = 0; i < len1; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
printf("Array2: ");
for (i = 0; i < len2; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
printf("Difference: "); for (i = 0; i < len1; i++) {
flag = 0;
for (j = 0; j < len2; j++) {
if (arr1[i] == arr2[j]) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("%d ", arr1[i]);
}
}
printf("\n");
return 0;
}
```
运行结果为:
```
Array1: 1 2 3 4 5
Array2: 3 4 5 6 7
Difference: 1 2
```
阅读全文