Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam2 / prob2.cpp
diff options
context:
space:
mode:
Commit3d7b742eb8e9012e21a0516ad21fc7aefd043bf7 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-01-11 10:09:44 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-01-11 10:09:44 +0200
Filesda790d25fcfa3251a8f57ee531c6a4fddd64804e /Year 1/Programming introduction/Exams/Exam2/prob2.cpp
uni-projects-3d7b742eb8e9012e21a0516ad21fc7aefd043bf7.tar
uni-projects-3d7b742eb8e9012e21a0516ad21fc7aefd043bf7.tar.gz
uni-projects-3d7b742eb8e9012e21a0516ad21fc7aefd043bf7.zip
Parenta6fd917db5d633aecfe0307b016108e78dd2f4f7 (diff)
Added my work from todays up exam
1 files changed, 60 insertions, 0 deletions
-rw-r--r--Year 1/Programming introduction/Exams/Exam2/prob2.cpp60
diff --git a/Year 1/Programming introduction/Exams/Exam2/prob2.cpp b/Year 1/Programming introduction/Exams/Exam2/prob2.cpp
new file mode 100644
index 0000000..72cb50e
--- /dev/null
+++ b/Year 1/Programming introduction/Exams/Exam2/prob2.cpp
@@ -0,0 +1,60 @@
+#include <iostream>
+
+const int MAX_SIZE = 30;
+
+bool hasRepeatedDigits(int n) {
+ for (int lastDigit = 0; n != 0; n /= 10) {
+ lastDigit = n % 10;
+ for (int tmp = n / 10; tmp != 0; tmp /= 10) {
+ if (tmp % 10 == lastDigit)
+ return true;
+ }
+ }
+ return false;
+}
+
+int getRepeatedLen(int matrix[MAX_SIZE][MAX_SIZE], int n) {
+ int arrSize = 0;
+ for (int col = 0, row = n - 1; col < n && row < n; col = (row > 0) ? 0 : col + 1, row = (row > 0) ? row - 1 : 0) {
+ for (int k = col, l = row; k < n && l < n; k++, l++) {
+ if (hasRepeatedDigits(matrix[l][k]))
+ arrSize++;
+ }
+ }
+ return arrSize;
+}
+
+int* collect(int matrix[MAX_SIZE][MAX_SIZE], int n) {
+ int arrSize = getRepeatedLen(matrix, n);
+ int *arr = new int[arrSize];
+
+ for (int col = 0, row = n - 1, arrI = 0; col < n && row < n; col = (row > 0) ? 0 : col + 1, row = (row > 0) ? row - 1 : 0) {
+ for (int k = col, l = row; k < n && l < n; k++, l++) {
+ if (hasRepeatedDigits(matrix[l][k]))
+ arr[arrI++] = matrix[l][k];
+ }
+ }
+
+ return arr;
+}
+
+int main() {
+ int n;
+ std::cin >> n;
+
+ int matrix[MAX_SIZE][MAX_SIZE];
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < n; j++)
+ std::cin >> matrix[i][j];
+ }
+
+ int *repeated = collect(matrix, n);
+ int repeatedLen = getRepeatedLen(matrix, n); // По условие няма как иначе да върнем размера
+
+ for (int i = 0; i < repeatedLen; i++) {
+ std::cout << repeated[i] << " ";
+ }
+ std::cout << std::endl;
+
+ delete[] repeated;
+}