求差集.c 850 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <stdio.h>
  2. #define MAX_SIZE 100
  3. int main() {
  4. int num;
  5. int A[MAX_SIZE], B[MAX_SIZE], C[MAX_SIZE];
  6. int sizeA = 0, sizeB = 0, sizeC = 0;
  7. // 读取集合A
  8. while (scanf("%d", &num) == 1 && num != -1) {
  9. A[sizeA++] = num;
  10. }
  11. // 读取集合B
  12. while (scanf("%d", &num) == 1 && num != -1) {
  13. B[sizeB++] = num;
  14. }
  15. // 计算差集 A - B
  16. int i, j, found;
  17. for (i = 0; i < sizeA; i++) {
  18. found = 0;
  19. for (j = 0; j < sizeB; j++) {
  20. if (A[i] == B[j]) {
  21. found = 1;
  22. break;
  23. }
  24. }
  25. if (!found) {
  26. C[sizeC++] = A[i];
  27. }
  28. }
  29. // 输出差集
  30. for (i = 0; i < sizeC; i++) {
  31. if (i > 0) printf(" ");
  32. printf("%d", C[i]);
  33. }
  34. printf("\n");
  35. return 0;
  36. }