Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.8 kB
1
Indexable
Never
#include<iostream>
using namespace std;

int main() {
	int n, m;
	do {
		cout << "nhap so phan tu cua mang 1: "; cin >> n; cout << endl;
		cout << "nhap so phan tu cua mang 2: "; cin >> m; cout << endl;
	} while (m < 0 || n < 0 || n>2020 || m>2020);

	int* a = new int[n];
	int* b = new int[m];
	for (int i = 0; i < n; i++) {
		cout << " nhap phan tu " << i << "cua mang 1: ";
		cin >> *(a + i);
		cout << endl;
	}
	for (int i = 0; i < m; i++) {
		cout << " nhap phan tu " << i << "cua mang 2: ";
		cin >> *(b + i);
		cout << endl;
	}

	int* chan = new int[100];
	int* le = new int[100];
	int x = 0, y = 0;
	for (int i = 0; i < n; i++) {
		if (*(a + i) % 2 == 1) {
			*(le + y) = *(a + i);
			y++;
		}
		else {
			*(chan + x) = *(a + i);
			x++;

		}
		for (int i = 0; i < m; i++) {
			if (*(b + i) % 2 == 1) {
				*(le + y) = *(b + i);
				y++;
			}
			else {
				*(chan + x) = *(b + i);
				x++;
			}
		}

		int* c = new int[100];
		if (x == 0 && y == 0)break;
		else if (x == 0 && y != 0) {
			c = le;
		}
		else if (y == 0 && x != 0) {
			c = chan;
		}
		else if (x > y) {
			int z = 0, k = 0;
			do {
				*(c + z) = *(le + k);
				*(c + z + 1) = *(chan + k);
				k++;
				z = z + 2;
			} while (k < y);
			for (int i = z; i < (m + n); i++) {
				*(c + z) = *(chan + k);
				k++;
			}
		}
		else if (x < y) {
			int z = 0, k = 0;
			do {
				*(c + z) = *(chan + k);
				*(c + z + 1) = *(le + k);
				k++;
				z = z + 2;
			} while (k < x);
			for (int i = z; i < (m + n); i++) {
				*(c + z) = *(le + k);
				k++;
			}
		}



		cout << "chuoi sau khi sap xep la: ";

		for (int i = 0; i < (100); i++) {
			cout << *(c + i) << " ";
		}
		delete[]chan;
		delete[]a;
		delete[]b;
		delete[]le;
		delete[]c;
		cout << endl;
		return 0;
	}
}