From 7881119e0914b9207568918fcf868eea84ef3d78 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Sat, 28 Nov 2020 01:45:44 +0200 Subject: sem 8 --- Sem. 08/README.md | 35 +++++++++++++++++++++++++++++++++++ Sem. 08/task1.cpp | 17 +++++++++++++++++ Sem. 08/task2.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ Sem. 08/task3.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 Sem. 08/README.md create mode 100644 Sem. 08/task1.cpp create mode 100644 Sem. 08/task2.cpp create mode 100644 Sem. 08/task3.cpp (limited to 'Sem. 08') 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 +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 +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 +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); + +} -- cgit v1.3.1-23-gd82d