Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 08
diff options
context:
space:
mode:
Commit7881119e0914b9207568918fcf868eea84ef3d78 (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2020-11-28 01:45:44 +0200
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2020-11-28 01:45:44 +0200
Filesab70701506971ec44b9aab9ba00ad9525b8da0e5 /Sem. 08
Introduction_to_programming_FMI-7881119e0914b9207568918fcf868eea84ef3d78.tar
Introduction_to_programming_FMI-7881119e0914b9207568918fcf868eea84ef3d78.tar.gz
Introduction_to_programming_FMI-7881119e0914b9207568918fcf868eea84ef3d78.zip
Parente5646c51794632f5816c00f4d1e7933ea7dcbbc0 (diff)
sem 8
4 files changed, 143 insertions, 0 deletions
-rw-r--r--Sem. 08/README.md35
-rw-r--r--Sem. 08/task1.cpp17
-rw-r--r--Sem. 08/task2.cpp42
-rw-r--r--Sem. 08/task3.cpp49
diff --git a/Sem. 08/README.md b/Sem. 08/README.md
new file mode 100644
index 0000000..e8c4d0b
--- /dev/null
+++ b/Sem. 08/README.md
@@ -0,0 +1,35 @@
+**Задача 1:** Въвежда се неотрицателно число n. Да се отпечатат всички двойки прости числа, които са във вида 6k-1 и 6k+1.
+
+**Задача 2:** Напишете функция, която приема неотрицателно число n и връща числото обърнато
+
+*Вход: 288, Изход: 822*
+
+**Задача 3:** Напишете функция, която приема неотрицателно число и връща дали числото е палиндром.
+
+**Задача 4:** Напишете функция, която приема неотрицателни числа n и k и връща дали k е суфикс на n.
+
+*Вход: 288, 88, Изход: true*
+
+**Задача 5:** Напишете функция, която приема неотрицателни числа n и k и връща дали k е префикс на n.
+
+*Вход: 288, 28, Изход: true*
+
+**Задача 6:** Напишете функция, която приема неотрицателни числа n и k и връща дали k се съдържа в n.
+
+*Вход: 2831, 83, Изход: true*
+
+**Задача 7:** Напишете функция, която приема неотрицателно число n и проверява дали има 4 последователни еднакви цифри.
+
+*Вход: 28777731, 83, Изход: true*
+
+**Задача 8:** Напишете функция, която приема неотрицателно число и връща дали цифрите му са сортирани във възходящ ред.
+
+*Вход: 122239, Изход: true*
+
+**Задача 9:** Напишете функция, която приема неотрицателно число и връща дали цифрите му са сортирани в низходящ ред.
+
+*Вход: 99943, Изход: true*
+
+**Задача 10:** Напишете функция, която приема неотрицателно число и връща дали цифрите му са еднакви.
+
+*Вход: 111111, Изход: true*
diff --git a/Sem. 08/task1.cpp b/Sem. 08/task1.cpp
new file mode 100644
index 0000000..f993c7e
--- /dev/null
+++ b/Sem. 08/task1.cpp
@@ -0,0 +1,17 @@
+#include <iostream>
+using namespace std;
+
+const int SIZE = 32;
+
+int Convert(bool arr[SIZE])
+{
+ int result = 0;
+ for (int i = 31; i >= 0; i--)
+ result += arr[i] * (1 << 31-i);
+ return result;
+}
+int main()
+{
+ bool arr[SIZE] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1 };
+ cout << Convert(arr);
+} \ No newline at end of file
diff --git a/Sem. 08/task2.cpp b/Sem. 08/task2.cpp
new file mode 100644
index 0000000..beb700d
--- /dev/null
+++ b/Sem. 08/task2.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+using namespace std;
+
+const int SIZE = 30;
+int CountOccurrences(int arr[SIZE], int n, int el)
+{
+ int count = 0;
+ for (int i = 0; i < n; i++)
+ {
+ if (arr[i] == el)
+ count++;
+ }
+ return count;
+}
+
+int GetLonlyNumber(int arr[SIZE], int n)
+{
+ for (int i = 0; i < n; i++)
+ {
+ if (CountOccurrences(arr, n, arr[i]) == 1)
+ return arr[i];
+ }
+ return -1;
+
+}
+
+int GetLonlyNumberBetter(int arr[SIZE], int n)
+{
+ int res = 0;
+ for (int i = 0; i < n; i++)
+ res ^= arr[i];
+ return res;
+}
+
+int main()
+{
+ int arr[SIZE];
+ int n;
+ for (int i = 0; i < n; i++)
+ cin >> arr[i];
+ cout << GetLonlyNumberBetter(arr, n);
+} \ No newline at end of file
diff --git a/Sem. 08/task3.cpp b/Sem. 08/task3.cpp
new file mode 100644
index 0000000..6d7d209
--- /dev/null
+++ b/Sem. 08/task3.cpp
@@ -0,0 +1,49 @@
+#include <iostream>
+using namespace std;
+
+const int SIZE = 30;
+bool isSubarrayPalindrome(int arr[SIZE], int startIndex, int endIndex)
+{
+ while (startIndex < endIndex)
+ {
+ if (arr[startIndex] != arr[endIndex])
+ return false;
+ startIndex++;
+ endIndex--;
+ }
+ return true;
+}
+void print(int arr[SIZE])
+{
+ for (int i = 0; i < SIZE; i++)
+ cout << arr[i];
+ cout << endl;
+}
+
+int LongestPalindromicSubarray(int arr[SIZE], int n)
+{
+ int currentSize = n;
+
+ int startIndex = 0;
+ int endIndex = startIndex + currentSize-1;
+ while (true)
+ {
+ if (isSubarrayPalindrome(arr, startIndex, endIndex))
+ return endIndex - startIndex + 1;
+ startIndex++;
+ endIndex++;
+ if (endIndex == n)//last iteration
+ {
+ currentSize--;
+ startIndex = 0;
+ endIndex = startIndex + currentSize - 1;
+ }
+ }
+}
+int main()
+{
+
+ int arr[SIZE] = {1,4,3,2,3};
+ cout << LongestPalindromicSubarray(arr, 5);
+
+}