Advertisement
huutho_96

Ví dụ

Dec 29th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. //Lưu ý bài tập này chỉ áp dụng cho đề này để đảm bảo tính tối ưu, tùy vào đề bài khác nhau nên có cách tiếp cận khác nhau
  2. //https://www.facebook.com/CungHocLapTrinhUIT
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. using namespace std;
  8.  
  9. void ReadFile(int *&A, int &n, int *&B, int &m)
  10. {
  11.     fstream f;
  12.     f.open("number5.in", ios::in | ios::out);
  13.     if (f)
  14.     {
  15.         f >> n >> m;
  16.         A = new int[n];
  17.         B = new int[m];
  18.         for (int i = 0; i < n; i++)
  19.             f >> A[i];
  20.         for (int i = 0; i < m; i++)
  21.             f >> B[i];
  22.         f.close();
  23.     }
  24.     else
  25.     {
  26.         cout << "File loi" << endl;
  27.     }
  28. }
  29.  
  30. void WriteFile(int arr[], int n)
  31. {
  32.     fstream f;
  33.     f.open("number5.out", ios::in | ios::out | ios::app);
  34.     if (f)
  35.     {
  36.         for (int i = 0; i < n; i++)
  37.             f << arr[i] << " ";
  38.         f << endl;
  39.         f.flush();
  40.         f.close();
  41.     }
  42.     else
  43.     {
  44.         cout << "File loi" << endl;
  45.     }
  46. }
  47.  
  48.  
  49. void Output(int A[], int n)
  50. {
  51.     for (int i = 0; i < n; i++)
  52.         cout << A[i] << "\t";
  53.     cout << endl;
  54. }
  55.  
  56.  
  57. //Sap xep va loai bo phan tu trung nhau ra khoi mang
  58. void Sort(int a[], int &n)
  59. {
  60.     for (int i = 0; i < n - 1; i++)
  61.     {
  62.         for (int j = i + 1; j < n; j++)
  63.         {
  64.             if (a[i] > a[j])
  65.             {
  66.                 a[i] = a[i] + a[j];
  67.                 a[j] = a[i] - a[j];
  68.                 a[i] = a[i] - a[j];
  69.             }
  70.             else
  71.             {
  72.                 if (a[i] == a[j])
  73.                 {
  74.                     a[n - 1] = a[n - 1] + a[j];
  75.                     a[j] = a[n - 1] - a[j];
  76.                     a[n - 1] = a[n - 1] - a[j];
  77.                     n--;
  78.                     j--;
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85.  
  86.  
  87. void Caub(int A[], int n, int B[], int m, int *&result, int &size)
  88. {
  89.     size = m + n;
  90.     result = new int[size];
  91.     for (int i = 0; i < n; i++)
  92.         result[i] = A[i];
  93.     for (int i = 0; i < m; i++)
  94.         result[n + i] = B[i];
  95.     Sort(result, size);
  96.     WriteFile(result, size);
  97. }
  98.  
  99. void Cauc(int A[], int n, int B[], int m, int *& result, int &size)
  100. {
  101.     size = 0;
  102.     int j = 0;
  103.     result = new int[n < m ? n : m];
  104.     for (int i = 0; i < n; i++)
  105.     {
  106.         if (A[i] == B[j])
  107.         {
  108.             result[size++] = A[i];
  109.             j++;
  110.         }
  111.         if (A[i] > B[j])
  112.         {
  113.             while (B[j] < A[i]) j++;
  114.             i--;
  115.         }
  116.     }
  117.     WriteFile(result, size);
  118. }
  119.  
  120. void Caud(int A[], int n, int B[], int m, int *& result, int &size)
  121. {
  122.     result = new int[n < m ? n : m];
  123.     size = 0;
  124.  
  125.     for (int i = 0; i < n; i++)
  126.     {
  127.         int j = m - 1;
  128.         while (B[j] > A[i]) j--;
  129.         if (B[j] < A[i])
  130.             result[size++] = A[i];
  131.     }
  132.     WriteFile(result, size);
  133. }
  134.  
  135. void main()
  136. {
  137.     int *A = NULL, *B = NULL, *result1 = NULL, *result2 = NULL, *result3 = NULL, *result4 = NULL;
  138.     int n, m, size1, size2, size3, size4;
  139.  
  140.     ReadFile(A, n, B, m);
  141.     Sort(A, n);
  142.     Sort(B, m);
  143.  
  144.     Caub(A, n, B, m, result1, size1);
  145.     Output(result1, size1);
  146.     size2 = n + m - size1;
  147.  
  148.     Cauc(A, n, B, m, result2, size2);
  149.     Output(result2, size2);
  150.  
  151.     Caud(A, n, B, m, result3, size3);
  152.     Output(result3, size3);
  153.  
  154.     Caud(B, m, A, n, result4, size4);
  155.     Output(result4, size4);
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement