From 2877ac7e174a13e5f8a914ccb8e9d277796321bd Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 28 Oct 2021 19:29:35 +0300 Subject: Solved all exercises from sem. 03 --- Sem. 03/Solutions/01. Digit_Sum.cpp | 14 +++++++++++ Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp | 17 ++++++++++++++ Sem. 03/Solutions/03. Remove_Middle_Digits.cpp | 24 +++++++++++++++++++ Sem. 03/Solutions/04. Primes_In_Interval.cpp | 30 ++++++++++++++++++++++++ Sem. 03/Solutions/05. Power_Of_Two.cpp | 14 +++++++++++ Sem. 03/Solutions/06. Polynomial_Solutions.cpp | 17 ++++++++++++++ Sem. 03/Solutions/08. Triangle.cpp | 16 +++++++++++++ Sem. 03/Solutions/09. Lefthand_Triangle.cpp | 22 +++++++++++++++++ Sem. 03/Solutions/10. Spiral_Numbers.cpp | 24 +++++++++++++++++++ Sem. 03/Solutions/11.Spiral_Numbers_2.cpp | 26 ++++++++++++++++++++ Sem. 03/Solutions/12. Frame.cpp | 17 ++++++++++++++ Sem. 03/Solutions/13. Dumbbell.cpp | 26 ++++++++++++++++++++ Sem. 03/Solutions/14. Tree.cpp | 26 ++++++++++++++++++++ 13 files changed, 273 insertions(+) create mode 100644 Sem. 03/Solutions/01. Digit_Sum.cpp create mode 100644 Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp create mode 100644 Sem. 03/Solutions/03. Remove_Middle_Digits.cpp create mode 100644 Sem. 03/Solutions/04. Primes_In_Interval.cpp create mode 100644 Sem. 03/Solutions/05. Power_Of_Two.cpp create mode 100644 Sem. 03/Solutions/06. Polynomial_Solutions.cpp create mode 100644 Sem. 03/Solutions/08. Triangle.cpp create mode 100644 Sem. 03/Solutions/09. Lefthand_Triangle.cpp create mode 100644 Sem. 03/Solutions/10. Spiral_Numbers.cpp create mode 100644 Sem. 03/Solutions/11.Spiral_Numbers_2.cpp create mode 100644 Sem. 03/Solutions/12. Frame.cpp create mode 100644 Sem. 03/Solutions/13. Dumbbell.cpp create mode 100644 Sem. 03/Solutions/14. Tree.cpp diff --git a/Sem. 03/Solutions/01. Digit_Sum.cpp b/Sem. 03/Solutions/01. Digit_Sum.cpp new file mode 100644 index 0000000..7bedf7f --- /dev/null +++ b/Sem. 03/Solutions/01. Digit_Sum.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() +{ + int n = 1, sum = 0; + while (n != 0) + { + cin >> n; + sum += n; + } + cout << sum << endl; +} diff --git a/Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp b/Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp new file mode 100644 index 0000000..75af7ea --- /dev/null +++ b/Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main() +{ + int n, k; + cin >> n >> k; + + // i = smaller number + for (int i = (n < k) ? n : k; i > 0; i--) + { + if (n % i == 0 && k % i == 0) + cout << i << " "; + } + cout << endl; +} diff --git a/Sem. 03/Solutions/03. Remove_Middle_Digits.cpp b/Sem. 03/Solutions/03. Remove_Middle_Digits.cpp new file mode 100644 index 0000000..5874ec4 --- /dev/null +++ b/Sem. 03/Solutions/03. Remove_Middle_Digits.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + int digits = 0, reversedN; + for (int i = n; i != 0; i /= 10) + digits++; + + int tenToTheHalf = 1; // ten to the power of digits to take from each end + int digitsToTakeFromEachEnd = digits / 2 + (digits % 2 - 1); + for (int i = 0; i < digitsToTakeFromEachEnd; i++) + tenToTheHalf *= 10; + + int tenToTheMiddle = (digits % 2 == 0) ? 100 : 10; // ten to the power of digits to remove from the middle + + int resultNumber = (n % tenToTheHalf) + (n / tenToTheHalf / tenToTheMiddle) * tenToTheHalf; + + cout << resultNumber << ", " << resultNumber + 1 << endl; +} diff --git a/Sem. 03/Solutions/04. Primes_In_Interval.cpp b/Sem. 03/Solutions/04. Primes_In_Interval.cpp new file mode 100644 index 0000000..a1510da --- /dev/null +++ b/Sem. 03/Solutions/04. Primes_In_Interval.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int main() +{ + int a, b; + cin >> a >> b; + + if (a < 2) a = 2; + + for (int i = a; i <= b; i++) + { + bool isPrime = true; + int rootOfI = sqrt(i); + for (int j = 2; j <= rootOfI; j++) + { + if (i % j == 0) + { + isPrime = false; + break; + } + } + + if (isPrime) + cout << i << " "; + } + cout << endl; +} diff --git a/Sem. 03/Solutions/05. Power_Of_Two.cpp b/Sem. 03/Solutions/05. Power_Of_Two.cpp new file mode 100644 index 0000000..9cbe442 --- /dev/null +++ b/Sem. 03/Solutions/05. Power_Of_Two.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + while(n % 2 == 0 && n > 1) + n /= 2; + + cout << ((n == 1) ? "true" : "false") << endl; +} diff --git a/Sem. 03/Solutions/06. Polynomial_Solutions.cpp b/Sem. 03/Solutions/06. Polynomial_Solutions.cpp new file mode 100644 index 0000000..375f6ec --- /dev/null +++ b/Sem. 03/Solutions/06. Polynomial_Solutions.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main() +{ + int a, b, c, d, e; + cin >> a >> b >> c >> d >> e; + + double fx; + for (int x = -100; x <= 100; x++) { + fx = a * x*x*x*x + b * x*x*x + c * x*x + d * x + e; + if(fx >= 0) + cout << x << " "; + } + cout << endl; +} diff --git a/Sem. 03/Solutions/08. Triangle.cpp b/Sem. 03/Solutions/08. Triangle.cpp new file mode 100644 index 0000000..72fec46 --- /dev/null +++ b/Sem. 03/Solutions/08. Triangle.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for(int i = 1; i <= n; i++) + { + for(int j = 0; j < i; j++) + cout << "$ "; + cout << endl; + } +} diff --git a/Sem. 03/Solutions/09. Lefthand_Triangle.cpp b/Sem. 03/Solutions/09. Lefthand_Triangle.cpp new file mode 100644 index 0000000..4d6e9b7 --- /dev/null +++ b/Sem. 03/Solutions/09. Lefthand_Triangle.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for(int i = -n + 1; i < n; i++) + { + int absI = ((i > 0) ? i : -i), number = 0; + + for(int j = n - absI; j > 0; j--) + (number *= 10) += j; + + for(int j = absI; j > 0; j--) + cout << " "; + + cout << number << endl; + } +} diff --git a/Sem. 03/Solutions/10. Spiral_Numbers.cpp b/Sem. 03/Solutions/10. Spiral_Numbers.cpp new file mode 100644 index 0000000..0b9d2a9 --- /dev/null +++ b/Sem. 03/Solutions/10. Spiral_Numbers.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for(int i = 0; i < n; i++) + { + int previous = i + 1; + for(int j = 0; j < n; j++) + { + if (j == 0) + cout << previous << " "; + else if (j % 2 == 1) + cout << (previous += (n-i-1) * 2 + 1) << " "; + else + cout << (previous += i*2 + 1) << " "; + } + cout << endl; + } +} diff --git a/Sem. 03/Solutions/11.Spiral_Numbers_2.cpp b/Sem. 03/Solutions/11.Spiral_Numbers_2.cpp new file mode 100644 index 0000000..3ff0f16 --- /dev/null +++ b/Sem. 03/Solutions/11.Spiral_Numbers_2.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for(int i = 0, previous = 0; i < n; i++) + { + int modifier = 1; + if (i % 2 == 1) + { + previous += n + 1; + modifier = -1; + } + + for(int j = 0; j < n; j++) + cout << (previous += modifier) << " "; + + cout << endl; + if (i % 2 == 1) + previous += n - 1; + } +} diff --git a/Sem. 03/Solutions/12. Frame.cpp b/Sem. 03/Solutions/12. Frame.cpp new file mode 100644 index 0000000..3d19376 --- /dev/null +++ b/Sem. 03/Solutions/12. Frame.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for(int i = 0; i < n; i++) + { + cout << ((i == 0 || i == n-1) ? "+" : "|"); + for(int j = 0; j < n - 2; j++) + cout << "-"; + cout << ((i == 0 || i == n-1) ? "+" : "|") << endl; + } +} diff --git a/Sem. 03/Solutions/13. Dumbbell.cpp b/Sem. 03/Solutions/13. Dumbbell.cpp new file mode 100644 index 0000000..d59cfac --- /dev/null +++ b/Sem. 03/Solutions/13. Dumbbell.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + int rowWidth = 5 * n / 2; + int evenness = 1 - n % 2; + for(int i = 0; i < n; i++) + { + if (i == 0 || i == n - 1) + { + for(int j = 0; j < rowWidth; j++) + cout << ((j == n || j == n + evenness) ? " " : "."); + } + else + { + for(int j = 0; j < rowWidth; j++) + cout << ((j == 1 || j == 1 + evenness || j == rowWidth - 2 || j == rowWidth - 2 - evenness) ? " " : "."); + } + cout << endl; + } +} diff --git a/Sem. 03/Solutions/14. Tree.cpp b/Sem. 03/Solutions/14. Tree.cpp new file mode 100644 index 0000000..b47daa4 --- /dev/null +++ b/Sem. 03/Solutions/14. Tree.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for(int i = 1; i <= n; i++) + { + for(int j = n - i; j > 0; j--) + cout << " "; + for(int j = -i + 1; j < i; j++) + { + cout << i - ((j < 0) ? -j : j); + } + cout << endl; + } + for(int i = n - 1; i > 0; i--) + { + for (int j = n - 1; j > 0; j--) + cout << " "; + cout << i << endl; + } +} -- cgit v1.3.1-23-gd82d