[Глава 4] Упражнения 1-4

This commit is contained in:
Пытков Роман
2025-07-26 17:46:42 +03:00
parent 1f144231c0
commit 83d5f60fe9
8 changed files with 72 additions and 3 deletions

View File

@@ -0,0 +1,19 @@
// Задача 4.1 - сумма массива в виде рекурсивной функции
int[] arr = [1, 2, 3];
Console.WriteLine($"4.1. Сумма: [{string.Join(", ", arr)}] равна {Task4_1.Calculate(arr)}");
// Задача 4.2 - количество элементов в списке в виде рекурсивной функции
LinkedList<int> list = new LinkedList<int>([10, 20, 30, 40, 50]);
Console.WriteLine($"4.2. Количество элементов в списке: {Task4_2.Count(list.First!)}");
// Задача 4.3 - поиск максимального элемента в LinkedList с помощью рекурсии
// Пример: находим максимум в списке [7, 2, 9, 4, 5]
LinkedList<int> list2 = new LinkedList<int>([7, 2, 9, 4, 5]);
Console.WriteLine($"4.3. Максимум в списке: {Task4_3.Max(list2.First!)}");
// Задача 4.4 - бинарный поиск в отсортированной коллекции с помощью рекурсии
int[] sortedArr = [10, 20, 30, 40, 50, 60];
int target = 42;
int index = Task4_4.BinSearch(sortedArr, target, 0, sortedArr.Length - 1);
Console.WriteLine($"4.4. Индекс числа {target} в массиве: {index}");

View File

@@ -0,0 +1,9 @@
static class Task4_1
{
public static int Calculate(int[] arr, int index = 0)
{
if (index == arr.Length - 1)
return arr[index];
return arr[index] + Calculate(arr, index+1);
}
}

View File

@@ -0,0 +1,9 @@
static class Task4_2
{
public static int Count<T>(LinkedListNode<T> listNode, int currentLen = 0)
{
if (listNode.Next != null)
return Count(listNode.Next, currentLen + 1);
return currentLen + 1;
}
}

View File

@@ -0,0 +1,10 @@
static class Task4_3
{
public static int Max(LinkedListNode<int> listNode, int currMax = int.MinValue)
{
currMax = Math.Max(currMax, listNode.Value);
if (listNode.Next != null)
return Max(listNode.Next, currMax);
return currMax;
}
}

View File

@@ -0,0 +1,16 @@
static class Task4_4
{
public static int BinSearch(IEnumerable<int> collection, int target, int left, int right)
{
if (left > right)
return left; // позиция для вставки
var mid = (left + right) / 2;
var value = collection.ElementAt(mid);
if (value == target)
return mid;
if (value < target)
return BinSearch(collection, target, mid + 1, right);
return BinSearch(collection, target, left, mid - 1);
}
}

View File

@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59 VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Recursion", "Recursion\Recursion.csproj", "{7F895D96-6E79-40BC-A778-89E76BC981F6}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter4_QuickSort", "Chapter4_QuickSort\Chapter4_QuickSort.csproj", "{4088F633-80E4-44DF-9DE4-996A4B3E3088}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -18,5 +18,13 @@ Global
{7F895D96-6E79-40BC-A778-89E76BC981F6}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F895D96-6E79-40BC-A778-89E76BC981F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.Build.0 = Release|Any CPU {7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.Build.0 = Release|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Release|Any CPU.Build.0 = Release|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@@ -1,2 +0,0 @@
Console.WriteLine("Hello, World!");