Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 15 / task3.cpp
diff options
context:
space:
mode:
Commit6be5815fbb76c2df984c660c4d1a99904a03ae8c (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-01-16 01:23:38 +0200
Committer
 GitHub <noreply@github.com>
2021-01-16 01:23:38 +0200
Files9accead6176630a7b64012094a15d25a71a82398 /Sem. 15/task3.cpp
Introduction_to_programming_FMI-6be5815fbb76c2df984c660c4d1a99904a03ae8c.tar
Introduction_to_programming_FMI-6be5815fbb76c2df984c660c4d1a99904a03ae8c.tar.gz
Introduction_to_programming_FMI-6be5815fbb76c2df984c660c4d1a99904a03ae8c.zip
Parent9a251b04502c200d972192e4af7f344dac0b3437 (diff)
Create task3.cpp
1 files changed, 69 insertions, 0 deletions
-rw-r--r--Sem. 15/task3.cpp69
diff --git a/Sem. 15/task3.cpp b/Sem. 15/task3.cpp
new file mode 100644
index 0000000..4b104cb
--- /dev/null
+++ b/Sem. 15/task3.cpp
@@ -0,0 +1,69 @@
+#include <iostream>
+#include <vector>
+#include <queue>
+using namespace std;
+
+//Зад1. Даден е символен низ (стринг). Сортираме думите лексикографски.
+
+// Вход: hello friends. how are you
+// Изход: are friends. hello how you
+
+// зад2. Даден е символен низ (стринг). Вярно ли е, че съдържа еднакви думи?
+
+// зад3. Дадени са два сортирани масива от цели числа.
+// Реализрайте функция, която приема двата масива и връща нов сортиран масив с елементите от двата масива.
+
+// Пример:
+// Масив1 : 4 8 10 66 70
+// Масив2 : 2 9 11 14
+// Резултат: 2 4 8 9 10 11 14 66 70
+
+// 1 начин - конкатенираме двата масива и прилагаме сортировка.
+// Губим предимството, че двата масива са били сортирани
+
+//2 начин-
+void merge(const int* arr1, int len1, const int* arr2, int len2, int* result)
+{
+ int ind1 = 0, ind2 = 0, resultIndex;
+
+ while (ind1 < len1 && ind2 < len2)
+ {
+ if (arr1[ind1] <= arr2[ind2])
+ result[resultIndex++] = arr1[ind1++];
+ else
+ result[resultIndex++] = arr2[ind2++];
+ }
+
+ while (ind1 < len1)//Ако е приключил първо вторият масив
+ result[resultIndex++] = arr1[ind1++];
+
+ while (ind2 < len2)//Ако е приключил първо първият масив
+ result[resultIndex++] = arr1[ind2++];
+
+}
+int main()
+{
+ int len1, len2;
+ cin >> len1 >> len2;
+
+ int* arr1 = new int[len1]; // (*)
+ int* arr2 = new int[len2]; // (**)
+ cout << "Enter SORTED array 1:" << endl;
+
+ for (int i = 0; i < len1; i++)
+ cin >> arr1[i];
+
+ cout << "Enter SORTED array 2:" << endl;
+
+ for (int i = 0; i < len2; i++)
+ cin >> arr2[i];
+
+ int* result = new int[len1 + len2]; // (***)
+
+ merge(arr1, len1, arr2, len2, result);
+
+ for (int i = 0; i < len1 + len2; i++)
+ cout << result[i] << " ";
+
+ delete[] arr1, arr2, result; // (*) (**) (***)
+}