diff options
feat(Week10): Add solutions
13 files changed, 553 insertions, 0 deletions
| -rw-r--r-- | Week10/Exercise01.cpp | 18 | ||||
| -rw-r--r-- | Week10/Exercise02.cpp | 22 | ||||
| -rw-r--r-- | Week10/Exercise03.cpp | 38 | ||||
| -rw-r--r-- | Week10/Exercise04.cpp | 62 | ||||
| -rw-r--r-- | Week10/Exercise05.cpp | 33 | ||||
| -rw-r--r-- | Week10/Exercise06.cpp | 45 | ||||
| -rw-r--r-- | Week10/Exercise07.cpp | 64 | ||||
| -rw-r--r-- | Week10/Exercise08.cpp | 50 | ||||
| -rw-r--r-- | Week10/Exercise09.cpp | 63 | ||||
| -rw-r--r-- | Week10/Exercise10.cpp | 71 | ||||
| -rw-r--r-- | Week10/Exercise11.cpp | 32 | ||||
| -rw-r--r-- | Week10/Exercise12.cpp | 52 | ||||
| -rw-r--r-- | Week10/README.md | 3 |
diff --git a/Week10/Exercise01.cpp b/Week10/Exercise01.cpp new file mode 100644 index 0000000..e9c9609 --- /dev/null +++ b/Week10/Exercise01.cpp @@ -0,0 +1,18 @@ +#include <iostream> + +int main() { + unsigned int n; + std::cin >> n; + + double* numbers = new double[n]; + for (unsigned int i = 0; i < n; i++) + std::cin >> numbers[i]; + + for (unsigned int i = n; i > 0; i--) + std::cout << numbers[i-1] << ' '; + std::cout << std::endl; + + delete[] numbers; + + return 0; +} diff --git a/Week10/Exercise02.cpp b/Week10/Exercise02.cpp new file mode 100644 index 0000000..88172ce --- /dev/null +++ b/Week10/Exercise02.cpp @@ -0,0 +1,22 @@ +#include <iostream> + +double average(int* arr, unsigned int size) { + double sum = 0; + for (unsigned int i = 0; i < size; i++) + sum += arr[i]; + return sum / size; +} + +int main() { + unsigned int n; + std::cin >> n; + n *= 2; + + int* nums = new int[n]; + for (unsigned int i = 0; i < n; i++) + std::cin >> nums[i]; + + std::cout << average(nums, n) << std::endl; + delete[] nums; + return 0; +} diff --git a/Week10/Exercise03.cpp b/Week10/Exercise03.cpp new file mode 100644 index 0000000..5815b58 --- /dev/null +++ b/Week10/Exercise03.cpp @@ -0,0 +1,38 @@ +#include <iostream> + +int main() { + unsigned int rows, cols; + std::cin >> rows >> cols; + + unsigned int** mat = new unsigned int*[rows]; + for (unsigned int row = 0; row < rows; row++) { + mat[row] = new unsigned int[cols]; + + for (unsigned int col = 0; col < cols; col++) + std::cin >> mat[row][col]; + } + + /* По принцип можем да решим задачата, все едно е динамична памет, + * тоест да разменяме първи елемент от първи ред с първи от последен + * ред, втори от първи ред с втори от последен ред и так. нат. + * + * Но тук, по-простия начин е да разменим указатели, което също е по-ефикасно. + */ + for (unsigned int row = 0; row < rows / 2; row++) { + unsigned int* temp = mat[row]; + mat[row] = mat[rows - 1 - row]; + mat[rows - 1 - row] = temp; + } + + for (unsigned int row = 0; row < rows; row++) { + for (unsigned int col = 0; col < cols; col++) + std::cout << mat[row][col] << ' '; + std::cout << std::endl; + } + + for (unsigned int row = 0; row < rows; row++) + delete[] mat[row]; + delete[] mat; + + return 0; +} diff --git a/Week10/Exercise04.cpp b/Week10/Exercise04.cpp new file mode 100644 index 0000000..5190bb4 --- /dev/null +++ b/Week10/Exercise04.cpp @@ -0,0 +1,62 @@ +#include <iostream> + +double** inputMatrix(unsigned int& rows, unsigned int& cols) { + std::cin >> rows >> cols; + + double** mat = new double*[rows]; + for (unsigned int row = 0; row < rows; row++) { + mat[row] = new double[cols]; + + for (unsigned int col = 0; col < cols; col++) + std::cin >> mat[row][col]; + } + + /* Използваме динамична памет, тя не се унищожава (освобождава) + * автоматично, което означава че можем да върнем указателя и + * всичко ще работи коректно. + */ + return mat; +} + +void deleteMatrix(double** mat, unsigned int rows) { + for (unsigned int row = 0; row < rows; row++) + delete[] mat[row]; + delete[] mat; +} + +int main() { + unsigned int rowsA, colsA, rowsB, colsB; + double** matA = inputMatrix(rowsA, colsA), **matB = inputMatrix(rowsB, colsB); + + if (colsA != rowsB) { + std::cout << "Cannot multiply!" << std::endl; + } + else { + double** matC = new double*[rowsA]; + + for (unsigned int row = 0; row < rowsA; row++) { + matC[row] = new double[colsB]; + + for (unsigned int col = 0; col < colsB; col++) { + matC[row][col] = 0; + + for (unsigned int i = 0; i < colsA; i++) { + matC[row][col] += matA[row][i] * matB[i][col]; + } + } + } + + for (unsigned int row = 0; row < rowsA; row++) { + for (unsigned int col = 0; col < colsB; col++) + std::cout << matC[row][col] << ' '; + std::cout << std::endl; + } + + deleteMatrix(matC, rowsA); + } + + deleteMatrix(matA, rowsA); + deleteMatrix(matB, rowsB); + + return 0; +} diff --git a/Week10/Exercise05.cpp b/Week10/Exercise05.cpp new file mode 100644 index 0000000..897ed4b --- /dev/null +++ b/Week10/Exercise05.cpp @@ -0,0 +1,33 @@ +#include <iostream> + +int largest(int** mat, unsigned int rows, unsigned int cols) { + int max = mat[0][0]; + + for (unsigned int row = 0; row < rows; row++) + for (unsigned int col = 0; col < cols; col++) + if (mat[row][col] > max) + max = mat[row][col]; + + return max; +} + +int main() { + int rows, cols; + std::cin >> rows >> cols; + + int** mat = new int*[rows]; + for (unsigned int row = 0; row < rows; row++) { + mat[row] = new int[cols]; + + for (unsigned int col = 0; col < cols; col++) + std::cin >> mat[row][col]; + } + + std::cout << largest(mat, rows, cols) << std::endl; + + for (unsigned int row = 0; row < rows; row++) + delete[] mat[row]; + delete[] mat; + + return 0; +} diff --git a/Week10/Exercise06.cpp b/Week10/Exercise06.cpp new file mode 100644 index 0000000..fdd62a7 --- /dev/null +++ b/Week10/Exercise06.cpp @@ -0,0 +1,45 @@ +#include <iostream> + +int main() { + unsigned int allocated = 4, elements = 0; + int* numbers = new int[allocated]; + + int temp; + std::cin >> temp; + while (temp != 0) { + numbers[elements++] = temp; + std::cin >> temp; + + /* Реоразмеряваме, ако не ни стига мястото + */ + if (elements >= allocated) { + allocated *= 2; + int* moreNumbers = new int[allocated]; + + for (unsigned int i = 0; i < elements; i++) + moreNumbers[i] = numbers[i]; + + delete[] numbers; + numbers = moreNumbers; + } + } + + if (elements == 1) { + std::cout << numbers[0] << std::endl; + } + else if (elements > 1) { + /* Нарочно използвам int, за да е по-разбираемо кои индекси ползваме, + * иначе този тип е грешен, понеже elements е unsigned int + */ + for (int i = elements - 2; i >= 0; i--) + numbers[i] += numbers[i + 1]; + + for (unsigned int i = 0; i < elements; i++) + std::cout << numbers[i] << ' '; + std::cout << std::endl; + } + + delete[] numbers; + + return 0; +} diff --git a/Week10/Exercise07.cpp b/Week10/Exercise07.cpp new file mode 100644 index 0000000..e6f2271 --- /dev/null +++ b/Week10/Exercise07.cpp @@ -0,0 +1,64 @@ +#include <iostream> + +bool** allocateMatrix(unsigned int rows, unsigned int cols) { + bool** mat = new bool*[rows]; + for (unsigned int row = 0; row < rows; row++) { + mat[row] = new bool[cols]; + for (unsigned int col = 0; col < cols; col++) + mat[row][col] = false; + } + + return mat; +} + +void deleteMatrix(bool** matrix, unsigned int rows) { + for (unsigned int row = 0; row < rows; row++) + delete[] matrix[row]; + delete[] matrix; +} + +int main() { + unsigned int allocatedRows = 4, allocatedCols = 4; + bool** mat = allocateMatrix(allocatedRows, allocatedCols); + + int row = 0, col = 0; + while (row >= 0 && col >= 0 && !mat[row][col]) { + mat[row][col] = true; + + char dir; + std::cin >> dir; + switch (dir) { + case 'r': col++; break; + case 'l': col--; break; + case 'u': row--; break; + case 'd': row++; break; + } + + if (row >= allocatedRows || col >= allocatedCols) { + bool** largerMat = allocateMatrix(allocatedRows * 2, allocatedCols * 2); + + for (unsigned int row = 0; row < allocatedRows; row++) + for (unsigned int col = 0; col < allocatedCols; col++) + largerMat[row][col] = mat[row][col]; + + deleteMatrix(mat, allocatedRows); + mat = largerMat; + + allocatedRows *= 2; + allocatedCols *= 2; + } + } + + unsigned int i, j; + std::cin >> i >> j; + if (i < allocatedRows && j < allocatedCols && mat[i][j]) { + std::cout << "Visited" << std::endl; + } + else { + std::cout << "Not visited" << std::endl; + } + + deleteMatrix(mat, allocatedRows); + + return 0; +} diff --git a/Week10/Exercise08.cpp b/Week10/Exercise08.cpp new file mode 100644 index 0000000..3d558e1 --- /dev/null +++ b/Week10/Exercise08.cpp @@ -0,0 +1,50 @@ +#include <iostream> + +void resizeArray(int*& arr, unsigned int& size) { + int* newArr = new int[size * 2]; + for (unsigned int i = 0; i < size; i++) + newArr[i] = arr[i]; + + delete[] arr; + arr = newArr; + size *= 2; +} + +int main() { + unsigned int countA = 0, sizeA = 4, countB = 0, sizeB = 4; + int* arrA = new int[sizeA], *arrB = new int[sizeB]; + + bool even = true; + int temp; + std::cin >> temp; + + while (temp > 0) { + if (even) { + arrA[countA++] = temp; + if (countA >= sizeA) + resizeArray(arrA, sizeA); + } + else { + arrB[countB++] = temp; + if (countB >= sizeB) + resizeArray(arrB, sizeB); + } + + even = !even; + std::cin >> temp; + } + + unsigned int count = (countA > countB) ? countA : countB; + for (unsigned int i = 0; i < count; i++) { + if (i < countB) + std::cout << arrB[countB - 1 - i] << ' '; + if (i < countA) + std::cout << arrA[i] << ' '; + } + std::cout << std::endl; + + delete[] arrA; + delete[] arrB; + + return 0; +} diff --git a/Week10/Exercise09.cpp b/Week10/Exercise09.cpp new file mode 100644 index 0000000..4a81925 --- /dev/null +++ b/Week10/Exercise09.cpp @@ -0,0 +1,63 @@ +#include <iostream> + +unsigned int* inputArray(unsigned int size) { + unsigned int* arr = new unsigned int[size]; + for (unsigned int i = 0; i < size; i++) + std::cin >> arr[i]; + return arr; +} + +void resizeArrayUINTPP(unsigned int**& arr, unsigned int size) { + unsigned int** newArr = new unsigned int*[size * 2]; + for (unsigned int i = 0; i < size; i++) + newArr[i] = arr[i]; + + delete[] arr; + arr = newArr; +} + +void resizeArrayUINTP(unsigned int*& arr, unsigned int size) { + unsigned int* newArr = new unsigned int[size * 2]; + for (unsigned int i = 0; i < size; i++) + newArr[i] = arr[i]; + + delete[] arr; + arr = newArr; +} + +int main() { + unsigned int jaggedCount = 0, jaggedSize = 4, mostColumns = 0; + unsigned int** jagged = new unsigned int*[jaggedSize], *rowSizes = new unsigned int[jaggedSize]; + + unsigned int size; + std::cin >> size; + while (size > 0) { + rowSizes[jaggedCount] = size; + jagged[jaggedCount++] = inputArray(size); + + if (size > mostColumns) + mostColumns = size; + + if (jaggedCount >= jaggedSize) { + resizeArrayUINTP(rowSizes, jaggedSize); + resizeArrayUINTPP(jagged, jaggedSize); + jaggedSize *= 2; + } + + std::cin >> size; + } + + for (unsigned int col = mostColumns; col > 0; col--) { + for (unsigned int row = 0; row < jaggedCount; row++) + if (col - 1 < rowSizes[row]) + std::cout << jagged[row][col - 1] << ' '; + std::cout << std::endl; + } + + for (unsigned int i = 0; i < jaggedSize; i++) + delete[] jagged[i]; + delete[] jagged; + delete[] rowSizes; + + return 0; +} diff --git a/Week10/Exercise10.cpp b/Week10/Exercise10.cpp new file mode 100644 index 0000000..706f689 --- /dev/null +++ b/Week10/Exercise10.cpp @@ -0,0 +1,71 @@ +#include <iostream> + +unsigned int** newHourglassMatrix(unsigned int size) { + unsigned int** mat = new unsigned int*[size]; + for (unsigned int row = 0; row < size / 2; row++) { + mat[row] = new unsigned int[size - row * 2]; + mat[size - 1 - row] = new unsigned int[size - row * 2]; + } + + if (size % 2 == 1) + mat[size / 2] = new unsigned int[1]; + return mat; +} + +void deleteHourglassMatrix(unsigned int** mat, unsigned int size) { + for (unsigned int row = 0; row < size / 2; row++) { + delete[] mat[row]; + delete[] mat[size - 1 - row]; + } + if (size % 2 == 1) + delete[] mat[size / 2]; + delete[] mat; +} + +void inputHourglassMatrix(unsigned int** mat, unsigned int size) { + for (unsigned int row = 0; row < size; row++) { + unsigned int cols = (row < size / 2) ? size - row * 2 : size - (size - 1 - row) * 2; + for (unsigned int col = 0; col < cols; col++) + std::cin >> mat[row][col]; + } +} + +void outputHourglassCountour(unsigned int** mat, unsigned int size) { + // Първи ред + for (unsigned int col = 0; col < size; col++) + std::cout << mat[0][col] << ' '; + + /* Втори диагонал */ + // От първи ред до преди средния изкарваме последния елемент на вс ред + for (unsigned int row = 1; row < size / 2; row++) + std::cout << mat[row][size - row * 2 - 1] << ' '; + // От средния ред до последния изкарваме първия елемент на вс ред + for (unsigned int row = size / 2; row < size; row++) + std::cout << mat[row][0] << ' '; + + // Последен ред + for (unsigned int col = 1; col < size; col++) + std::cout << mat[size - 1][col] << ' '; + + /* Главен диагонал */ + // От последния ред до средния изкарваме последния елемент на вс ред + for (unsigned int row = size - 2; row >= size / 2; row--) + std::cout << mat[row][size - (size - 1 - row) * 2 - 1] << ' '; + // От преди средния ред до първия ред изкарваме първия елемент на вс ред + for (unsigned int row = size / 2; row > 0; row--) + std::cout << mat[row - 1][0] << ' '; + + std::cout << std::endl; +} + +int main() { + unsigned int size; + std::cin >> size; + unsigned int** hmat = newHourglassMatrix(size); + inputHourglassMatrix(hmat, size); + + outputHourglassCountour(hmat, size); + + deleteHourglassMatrix(hmat, size); + return 0; +} diff --git a/Week10/Exercise11.cpp b/Week10/Exercise11.cpp new file mode 100644 index 0000000..a906855 --- /dev/null +++ b/Week10/Exercise11.cpp @@ -0,0 +1,32 @@ +#include <iostream> + +int main() { + unsigned int n; + std::cin >> n; + + double* coeffs = new double[n + 1]; + for (unsigned int i = 0; i < n + 1; i++) + std::cin >> coeffs[i]; + + double potentialRoot; + std::cin >> potentialRoot; + + double sum = 0.0, toPower = 1.0; + for (unsigned int i = n + 1; i > 0; i--) { + sum += coeffs[i-1] * toPower; + toPower *= potentialRoot; + } + + const double ZERO_BORDER = 0.00001; + + if (-ZERO_BORDER < sum && sum < ZERO_BORDER) { + std::cout << "Is a root" << std::endl; + } + else { + std::cout << "Not a root" << std::endl; + } + + delete[] coeffs; + + return 0; +} diff --git a/Week10/Exercise12.cpp b/Week10/Exercise12.cpp new file mode 100644 index 0000000..38bf452 --- /dev/null +++ b/Week10/Exercise12.cpp @@ -0,0 +1,52 @@ +#include <iostream> + +int main() { + unsigned int count = 0, allocated = 4; + int* indecies = new int[allocated]; + double* values = new double[allocated]; + + int index; + std::cin >> index; + while (index >= 0) { + indecies[count] = index; + std::cin >> values[count++]; + + if (count >= allocated) { + allocated *= 2; + int* newIndecies = new int[allocated]; + double* newValues = new double[allocated]; + + for (unsigned int i = 0; i < count; i++) { + newIndecies[i] = indecies[i]; + newValues[i] = values[i]; + } + + delete[] indecies; + delete[] values; + + indecies = newIndecies; + values = newValues; + } + + std::cin >> index; + } + + std::cin >> index; + bool printed = false; + + for (unsigned int i = 0; i < count; i++) { + if (indecies[i] == index) { + std::cout << values[i] << std::endl; + printed = true; + break; + } + } + + if (!printed) + std::cout << 0 << std::endl; + + delete[] indecies; + delete[] values; + + return 0; +} diff --git a/Week10/README.md b/Week10/README.md new file mode 100644 index 0000000..290bded --- /dev/null +++ b/Week10/README.md @@ -0,0 +1,3 @@ +# Задачи + +<https://syndamia.com/teaching/upp-2025/week10/> |
