Added my works from 2022

This commit is contained in:
2024-11-21 09:11:52 +03:00
parent facd000301
commit 02068890a5
7 changed files with 161 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.out
.ccls
.ccls-cache

27
alg_4_1.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <iostream>
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);
}

17
alg_4_2.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include <iostream>
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);
}

55
alg_4_3.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include <iostream>
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;
}

18
alg_4_4.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include <iostream>
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);
}

15
alg_4_5.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include <iostream>
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);
}

26
alg_4_6.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <iostream>
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);
}