Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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
- //https://www.facebook.com/CungHocLapTrinhUIT
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- void ReadFile(int *&A, int &n, int *&B, int &m)
- {
- fstream f;
- f.open("number5.in", ios::in | ios::out);
- if (f)
- {
- f >> n >> m;
- A = new int[n];
- B = new int[m];
- for (int i = 0; i < n; i++)
- f >> A[i];
- for (int i = 0; i < m; i++)
- f >> B[i];
- f.close();
- }
- else
- {
- cout << "File loi" << endl;
- }
- }
- void WriteFile(int arr[], int n)
- {
- fstream f;
- f.open("number5.out", ios::in | ios::out | ios::app);
- if (f)
- {
- for (int i = 0; i < n; i++)
- f << arr[i] << " ";
- f << endl;
- f.flush();
- f.close();
- }
- else
- {
- cout << "File loi" << endl;
- }
- }
- void Output(int A[], int n)
- {
- for (int i = 0; i < n; i++)
- cout << A[i] << "\t";
- cout << endl;
- }
- //Sap xep va loai bo phan tu trung nhau ra khoi mang
- void Sort(int a[], int &n)
- {
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = i + 1; j < n; j++)
- {
- if (a[i] > a[j])
- {
- a[i] = a[i] + a[j];
- a[j] = a[i] - a[j];
- a[i] = a[i] - a[j];
- }
- else
- {
- if (a[i] == a[j])
- {
- a[n - 1] = a[n - 1] + a[j];
- a[j] = a[n - 1] - a[j];
- a[n - 1] = a[n - 1] - a[j];
- n--;
- j--;
- }
- }
- }
- }
- }
- void Caub(int A[], int n, int B[], int m, int *&result, int &size)
- {
- size = m + n;
- result = new int[size];
- for (int i = 0; i < n; i++)
- result[i] = A[i];
- for (int i = 0; i < m; i++)
- result[n + i] = B[i];
- Sort(result, size);
- WriteFile(result, size);
- }
- void Cauc(int A[], int n, int B[], int m, int *& result, int &size)
- {
- size = 0;
- int j = 0;
- result = new int[n < m ? n : m];
- for (int i = 0; i < n; i++)
- {
- if (A[i] == B[j])
- {
- result[size++] = A[i];
- j++;
- }
- if (A[i] > B[j])
- {
- while (B[j] < A[i]) j++;
- i--;
- }
- }
- WriteFile(result, size);
- }
- void Caud(int A[], int n, int B[], int m, int *& result, int &size)
- {
- result = new int[n < m ? n : m];
- size = 0;
- for (int i = 0; i < n; i++)
- {
- int j = m - 1;
- while (B[j] > A[i]) j--;
- if (B[j] < A[i])
- result[size++] = A[i];
- }
- WriteFile(result, size);
- }
- void main()
- {
- int *A = NULL, *B = NULL, *result1 = NULL, *result2 = NULL, *result3 = NULL, *result4 = NULL;
- int n, m, size1, size2, size3, size4;
- ReadFile(A, n, B, m);
- Sort(A, n);
- Sort(B, m);
- Caub(A, n, B, m, result1, size1);
- Output(result1, size1);
- size2 = n + m - size1;
- Cauc(A, n, B, m, result2, size2);
- Output(result2, size2);
- Caud(A, n, B, m, result3, size3);
- Output(result3, size3);
- Caud(B, m, A, n, result4, size4);
- Output(result4, size4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement