fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int comp(const void* a_, const void* b_) {
  6. return *(int*)a_ - *(int*)b_;
  7. }
  8.  
  9. int main() {
  10. int N = 20;
  11. int* A = malloc(N * sizeof(int));
  12. int* B = malloc(N * sizeof(int));
  13. int* C = malloc(N * sizeof(int));
  14.  
  15. for (int i = 0; i < N; ++i) {
  16. A[i] = rand() % 30;
  17. B[i] = rand() % 30;
  18. }
  19.  
  20. for (int i = 0; i < N; ++i) printf("%d ", A[i]);
  21.  
  22. puts("");
  23.  
  24. for (int i = 0; i < N; ++i) printf("%d ", B[i]);
  25.  
  26. puts("");
  27. // Отсортировали
  28. qsort(A, N, sizeof(int), comp);
  29. qsort(B, N, sizeof(int), comp);
  30.  
  31. puts("");
  32. for (int i = 0; i < N; ++i) printf("%d ", A[i]);
  33. puts("");
  34. for (int i = 0; i < N; ++i) printf("%d ", B[i]);
  35. puts("");
  36.  
  37. // Выбросили дубли
  38. int* a = A, *b = B;
  39. int NA, NB;
  40.  
  41. for (int* c = a; ++a < A + N; NA = c - A + 1)
  42. if (*c != *a && ++c != a) * c = *a;
  43.  
  44. for (int* c = b; ++b < B + N; NB = c - B + 1)
  45. if (*c != *b && ++c != b) * c = *b;
  46.  
  47. puts("");
  48. for (int i = 0; i < NA; ++i) printf("%d ", A[i]);
  49. puts("");
  50. for (int i = 0; i < NB; ++i) printf("%d ", B[i]);
  51. puts("");
  52.  
  53. // Находим разность отсортированных смножеств
  54. a = A;
  55. b = B;
  56. int j = 0;
  57.  
  58. while (a < A + NA) {
  59. if (b == B + NB) {
  60. for (; a < A + NA;) C[j++] = *a++;
  61.  
  62. break;
  63. }
  64.  
  65. if (*a < *b) C[j++] = *a++;
  66. else {
  67. if (*b >= *a) a++;
  68.  
  69. b++;
  70. }
  71. }
  72.  
  73. printf("%d\n", j);
  74. for (int i = 0; i < j; ++i) printf("%d ", C[i]);
  75. puts("");
  76. }
  77.  
  78.  
Success #stdin #stdout 0s 4936KB
stdin
Standard input is empty
stdout
13 27 23 16 9 2 20 23 0 22 11 27 2 2 7 29 12 29 13 1 
16 25 25 12 1 7 19 16 6 16 8 9 20 13 25 12 18 27 16 22 

0 1 2 2 2 7 9 11 12 13 13 16 20 22 23 23 27 27 29 29 
1 6 7 8 9 12 12 13 16 16 16 16 18 19 20 22 25 25 25 27 

0 1 2 7 9 11 12 13 16 20 22 23 27 29 
1 6 7 8 9 12 13 16 18 19 20 22 25 27 
5
0 2 11 23 29