diff options
Add Pract. solutions.
8 files changed, 144 insertions, 0 deletions
| -rw-r--r-- | pract_SI/Pract. 09/task1.cpp | 10 | ||||
| -rw-r--r-- | pract_SI/Pract. 09/task2.cpp | 23 | ||||
| -rw-r--r-- | pract_SI/Pract. 09/task3.cpp | 10 | ||||
| -rw-r--r-- | pract_SI/Pract. 09/task4.cpp | 15 | ||||
| -rw-r--r-- | pract_SI/Pract. 09/task5.cpp | 21 | ||||
| -rw-r--r-- | pract_SI/Pract. 09/task6.cpp | 18 | ||||
| -rw-r--r-- | pract_SI/Pract. 09/task7.cpp | 32 | ||||
| -rw-r--r-- | pract_SI/Pract. 09/task9.cpp | 15 |
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 <iostream> + +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 <vector> + +void insertIntoVector(std::vector<int>& 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 <vector> + +bool checkIsPalindrom(const vector<int>& 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<int>& v, int operations) +{ + if(v.empty()) + return false; + + if (operations < 0) + return false; + + if (checkIsPalindrom(v)) + return true; + + std::vector<int> 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 |
