补集
假设集合A为集合B的子集,存在于集合B但不存在于集合A的元素的集合叫作集合A在集合B中的补集
代码如下:
#include <stdio.h>
#include <stdbool.h>
#define FULL_LEN 5
#define ARRAY_LEN 3
#define COMPLEMENTARY_LEN FULL_LEN - ARRAY_LEN
int main() {
int full[FULL_LEN] = {1, 2, 3, 4, 5};
int array[ARRAY_LEN] = {1, 2, 3};
int res[COMPLEMENTARY_LEN] = {0};
int res_i = 0;
bool is_exist;
for (int i = 0; i < FULL_LEN; ++i) {
is_exist = false;
for (int j = 0; j < ARRAY_LEN; ++j) {
if (full[i] == array[j]) {
is_exist = true;
break;
}
}
if (is_exist == false) {
res[res_i] = full[i];
++res_i;
}
}
printf("补集:[");
bool is_first = true;
for (int i = 0; i < COMPLEMENTARY_LEN; ++i) {
if (is_first == true) {
printf("%d", res[i]);
is_first = false;
} else {
printf(", %d", res[i]);
}
}
printf("]\n");
return 0;
}