From 97b4d62734acf7533a96b08bcc6044eb94847931 Mon Sep 17 00:00:00 2001 From: Justsvetoslavov Date: Thu, 13 Jan 2022 21:15:47 +0200 Subject: Add Pract. solutions. --- pract_SI/Pract. 09/task1.cpp | 10 ++++++++++ pract_SI/Pract. 09/task2.cpp | 23 +++++++++++++++++++++++ pract_SI/Pract. 09/task3.cpp | 10 ++++++++++ pract_SI/Pract. 09/task4.cpp | 15 +++++++++++++++ pract_SI/Pract. 09/task5.cpp | 21 +++++++++++++++++++++ pract_SI/Pract. 09/task6.cpp | 18 ++++++++++++++++++ pract_SI/Pract. 09/task7.cpp | 32 ++++++++++++++++++++++++++++++++ pract_SI/Pract. 09/task9.cpp | 15 +++++++++++++++ 8 files changed, 144 insertions(+) create mode 100644 pract_SI/Pract. 09/task1.cpp create mode 100644 pract_SI/Pract. 09/task2.cpp create mode 100644 pract_SI/Pract. 09/task3.cpp create mode 100644 pract_SI/Pract. 09/task4.cpp create mode 100644 pract_SI/Pract. 09/task5.cpp create mode 100644 pract_SI/Pract. 09/task6.cpp create mode 100644 pract_SI/Pract. 09/task7.cpp create mode 100644 pract_SI/Pract. 09/task9.cpp diff --git a/pract_SI/Pract. 09/task1.cpp b/pract_SI/Pract. 09/task1.cpp new file mode 100644 index 0000000..1763817 --- /dev/null +++ b/pract_SI/Pract. 09/task1.cpp @@ -0,0 +1,10 @@ +bool Exist_digit(unsigned int val, unsigned int digit) +{ + if (val == 0) + return false; + + if (val % 10 == digit) + return true; + + Exist_digit(val / 10, digit) +} \ No newline at end of file diff --git a/pract_SI/Pract. 09/task2.cpp b/pract_SI/Pract. 09/task2.cpp new file mode 100644 index 0000000..e52d12d --- /dev/null +++ b/pract_SI/Pract. 09/task2.cpp @@ -0,0 +1,23 @@ +//Way 1 +unsigned Pow(unsigned x, unsigned n) +{ + if (n != 0) + return x * Pow(x, n - 1); + + return 1; +} + +//Way 2 - more optional +unsigned power(unsigned x, unsigned n) +{ + if (n == 0) + return 1; + + if (n % 2 == 1) + return x * power(x, n - 1); + else + { + unsigned temp = power(x, n / 2); + return temp * temp; + } +} \ No newline at end of file diff --git a/pract_SI/Pract. 09/task3.cpp b/pract_SI/Pract. 09/task3.cpp new file mode 100644 index 0000000..baf8da0 --- /dev/null +++ b/pract_SI/Pract. 09/task3.cpp @@ -0,0 +1,10 @@ +#include + +void reversedNumber(int n) +{ + if(n == 0) + return; + + std::cout << n % 10; + return reversedNumber(n / 10); +} diff --git a/pract_SI/Pract. 09/task4.cpp b/pract_SI/Pract. 09/task4.cpp new file mode 100644 index 0000000..d89c934 --- /dev/null +++ b/pract_SI/Pract. 09/task4.cpp @@ -0,0 +1,15 @@ +bool Is_monotonically_increasing(const float* seq,int length,int position) +{ + if(position==length-1) + return true; + if(seq[position]>seq[position+1]) return false; + return Is_monotonically_increasing(seq,length,position+1); +} + +bool Is_rising(const float* seq,int Size) +{ + if(Size<2) return true; + if(seq>(seq+1)) return false; + /// if(seq[0]>seq[1]) return false; -same + return Is_rising(++seq,Size-1); +} \ No newline at end of file diff --git a/pract_SI/Pract. 09/task5.cpp b/pract_SI/Pract. 09/task5.cpp new file mode 100644 index 0000000..bd11733 --- /dev/null +++ b/pract_SI/Pract. 09/task5.cpp @@ -0,0 +1,21 @@ +//Way 1 - 3 parameters(array,size,position/index) +bool Is_monotonically_increasing(const float* seq,int length,int position) +{ + if(position==length-1) + return true; + if(seq[position]>seq[position+1]) + return false; + return Is_monotonically_increasing(seq,length,position+1); +} + +//Way 2 - 2 parameters(array + size) +bool Is_rising(const float* seq,int Size) +{ + if(Size<2) + return true; + + if(*seq>*(seq+1)) ///Same as if(seq[0]>seq[1]) return false; + return false; + + return Is_rising(++seq,Size-1); +} \ No newline at end of file diff --git a/pract_SI/Pract. 09/task6.cpp b/pract_SI/Pract. 09/task6.cpp new file mode 100644 index 0000000..a380fb2 --- /dev/null +++ b/pract_SI/Pract. 09/task6.cpp @@ -0,0 +1,18 @@ +#include + +void insertIntoVector(std::vector& arr, int number, int index) +{ + if (arr.empty() || arr.size() <= index) //insert @end + { + arr.push_back(number); + return; + } + + if (arr[index] > number) + { + arr.insert(arr.begin() + index, number); + return; + } + + return insertIntoVector(arr, number, ++index); +} diff --git a/pract_SI/Pract. 09/task7.cpp b/pract_SI/Pract. 09/task7.cpp new file mode 100644 index 0000000..5065a6d --- /dev/null +++ b/pract_SI/Pract. 09/task7.cpp @@ -0,0 +1,32 @@ +#include + +bool checkIsPalindrom(const vector& v) +{ + //if (v.size() < 2) - not needed as for-loop covers this case. + // return true; + + for (size_t i = 0; i < v.size() / 2; i++) + if (v[i] != v[v.size() - 1 - i]) + return false; + + return true; +} + +bool isPalindrom(const std::vector& v, int operations) +{ + if(v.empty()) + return false; + + if (operations < 0) + return false; + + if (checkIsPalindrom(v)) + return true; + + std::vector left = v, right = v; + left.erase(left.begin()); //remove first + right.pop_back(); //remove last + operations--; + + return isPalindrom(left, operations) || isPalindrom(right, operations); +} \ No newline at end of file diff --git a/pract_SI/Pract. 09/task9.cpp b/pract_SI/Pract. 09/task9.cpp new file mode 100644 index 0000000..2c0bd58 --- /dev/null +++ b/pract_SI/Pract. 09/task9.cpp @@ -0,0 +1,15 @@ +void printLeapYears(unsigned year, unsigned N) +{ + if (N == 0) + return; + + if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) + { + cout << year << endl; + return printLeapYears(year + 4, --N); + } + else if(year % 100 == 0) + return printLeapYears(year + 4, N); + + return printLeapYears(year + 1, N); +} \ No newline at end of file -- cgit v1.3.1-23-gd82d