diff options
Add files via upload
3 files changed, 144 insertions, 0 deletions
| -rw-r--r-- | Sem. 15/task1.cpp | 46 | ||||
| -rw-r--r-- | Sem. 15/task2.cpp | 50 | ||||
| -rw-r--r-- | Sem. 15/task3.cpp | 48 |
diff --git a/Sem. 15/task1.cpp b/Sem. 15/task1.cpp new file mode 100644 index 0000000..502b7a5 --- /dev/null +++ b/Sem. 15/task1.cpp @@ -0,0 +1,46 @@ +#include <iostream>
+
+int min(int a, int b)
+{
+ return a < b ? a : b;
+}
+int min(int a, int b, int c)
+{
+ return min(min(a, b), c);
+}
+int getMinOperations(size_t N, int* cache)
+{
+ if (N == 1)
+ return 0;
+
+ if (cache[N] != 0)
+ return cache[N];
+
+ int dvBy3 = INT_MAX;
+ int dvBy2 = INT_MAX;
+ int s1 = INT_MAX;
+
+ if (N % 3 == 0)
+ dvBy3 = getMinOperations(N / 3, cache);
+ if (N % 2 == 0)
+ dvBy2 = getMinOperations(N / 2, cache);
+ s1 = getMinOperations(N - 1, cache);
+
+ int result = 1 + min(dvBy2, dvBy3, s1);
+ cache[N] = result;
+ return result;
+}
+
+int getMinOperationsWrapper(size_t N)
+{
+ int* cache = new int[N + 1];
+ for (size_t i = 0; i < N + 1; i++)
+ cache[i] = 0;
+
+ return getMinOperations(N, cache);
+}
+
+int main()
+{
+ std::cout << getMinOperationsWrapper(19);// 1 -> 3 -> 6 -> 18 -> 19
+}
\ No newline at end of file diff --git a/Sem. 15/task2.cpp b/Sem. 15/task2.cpp new file mode 100644 index 0000000..55229c2 --- /dev/null +++ b/Sem. 15/task2.cpp @@ -0,0 +1,50 @@ +#include <iostream>
+
+
+bool isValidSymbol(char ch)
+{
+ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9');
+}
+//true word1 < word2
+bool isLexLess(const char* word1, const char* word2)
+{
+ while (isValidSymbol(*word1) && isValidSymbol(*word2))
+ {
+ if (*word1 < *word2)
+ return true;
+ else if (*word1 > *word2)
+ return false;
+ word1++;
+ word2++;
+ }
+ return !isValidSymbol(*word1);
+}
+
+
+const char* minWord(const char* str)
+{
+ size_t len = strlen(str);
+ const char* currentMin = nullptr;
+ const char* iter = str;
+ while (*iter != '\0')
+ {
+ if (isValidSymbol(*iter) && ((iter == str) || !isValidSymbol(*(iter - 1))))
+ {
+ if (currentMin == nullptr)
+ {
+ currentMin = iter;
+ }
+ else
+ {
+ if (isLexLess(iter, currentMin))
+ currentMin = iter;
+ }
+ }
+ iter++;
+ }
+ return currentMin;
+}
+int main()
+{
+ std::cout << minWord("me? why always me");// 1 -> 3 -> 6 -> 18 -> 19
+}
\ No newline at end of file diff --git a/Sem. 15/task3.cpp b/Sem. 15/task3.cpp new file mode 100644 index 0000000..83bc8f3 --- /dev/null +++ b/Sem. 15/task3.cpp @@ -0,0 +1,48 @@ +#include <iostream>
+
+
+bool isLowerChar(char ch)
+{
+ return ch >= 'a' && ch <= 'z';
+}
+char* sortLowerChars(const char* str)
+{
+ const int ALPHABET_SIZE = 26;
+ int hist[ALPHABET_SIZE] = { 0 };
+ int newSize = 0;
+
+ while (*str != '\0')
+ {
+ if (isLowerChar(*str))
+ {
+ newSize++;
+ hist[*str - 'a']++;
+ }
+
+ str++;
+ }
+
+ char* result = new char[newSize + 1];
+ result[newSize] = '\0';
+ int resultIndex = 0;
+
+ for (int i = 0; i < ALPHABET_SIZE; i++)
+ {
+ while (hist[i] > 0)
+ {
+ result[resultIndex++] = 'a' + i;
+ hist[i]--;
+ }
+ }
+ return result;
+
+}
+
+int main()
+{
+ char* result = sortLowerChars("bbzzAaaTYb");
+
+ std::cout << result << std::endl;
+
+ delete[] result;
+}
\ No newline at end of file |
