diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e624d96 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.out +.ccls +.ccls-cache diff --git a/alg_4_1.cpp b/alg_4_1.cpp new file mode 100644 index 0000000..b6bad89 --- /dev/null +++ b/alg_4_1.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +int gcd_recur(int n1, int n2) +{ + if (n1 == n2) + { + return n1; + } + + if (n1 > n2) + { + return gcd_recur(n1-n2, n2); + } + else + { + return gcd_recur(n1, n2-n1); + } +} + +int main() +{ + int a, b; + cin >> a >> b; + + cout << gcd_recur(a, b); +} \ No newline at end of file diff --git a/alg_4_2.cpp b/alg_4_2.cpp new file mode 100644 index 0000000..4b2f7c5 --- /dev/null +++ b/alg_4_2.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int fib(int n) +{ + if (n == 0) { return 1; } + if (n == 1) { return 1; } + + return fib(n - 1) + fib(n - 2); +} + +int main() +{ + int n; cin >> n; + + cout << fib(n); +} \ No newline at end of file diff --git a/alg_4_3.cpp b/alg_4_3.cpp new file mode 100644 index 0000000..c20addc --- /dev/null +++ b/alg_4_3.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; + +double root(int n) +{ + double L, R, B; + L = 0; R = n; + + while (fabs(R - L) > 0.00001) + { + B = (L + R) / 2; + + if ( B*B > n) + { + R = B; + } + else { + L = B; + } + } + return B; +} + +double root_hf_div(double n, double L, double R) +{ + double B; + + if (fabs(R - L) < 0.00001) + { + return (L + R) / 2; + } + else + { + B = (L + R) / 2; + + if ((B * B) > n) + { + return root_hf_div(n, L, B); + } + else + { + return root_hf_div(n, B, R); + } + } +} + +int main() +{ + setlocale(LC_ALL, "Russian"); + double n; cin >> n; + + cout << "Обычный: " << root(n) << endl; + + cout << "Рекурсивный: " << root_hf_div(n, 1, n) << endl; +} \ No newline at end of file diff --git a/alg_4_4.cpp b/alg_4_4.cpp new file mode 100644 index 0000000..3507c63 --- /dev/null +++ b/alg_4_4.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int f(int n, int sum) +{ + if (n == 0) + { + return sum; + } + return f(n / 10, sum + n % 10); +} + +int main() +{ + int n; cin >> n; + + cout << f(n, 0); +} \ No newline at end of file diff --git a/alg_4_5.cpp b/alg_4_5.cpp new file mode 100644 index 0000000..f3e65ed --- /dev/null +++ b/alg_4_5.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int f(int n) +{ + if (n == 1) { return 1; } + if (n % 2 == 0) { return f(n / 2); } + return f((n - 1) / 2) + f((n - 1) / 2 + 1); +} + +int main() +{ + int n; cin >> n; + cout << f(n); +} \ No newline at end of file diff --git a/alg_4_6.cpp b/alg_4_6.cpp new file mode 100644 index 0000000..ed68bde --- /dev/null +++ b/alg_4_6.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +void move(int a, int b, int c, int n) +{ + if (n == 1) + { + cout << "from " << a << " to " << c << endl; + } + else + { + move(a, c, b, n - 1); + + cout << "from " << a << " to " << c << endl; + + move(b, a, c, n - 1); + } +} + +int main() +{ + int n; + cout << "Enter the amount of disks: "; cin >> n; + + move(1, 2, 3, n); +} \ No newline at end of file