diff --git a/Lab3/Lab3.csproj b/Lab3/Lab3.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Lab3/Lab3.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Lab3/Program.cs b/Lab3/Program.cs new file mode 100644 index 0000000..afac41d --- /dev/null +++ b/Lab3/Program.cs @@ -0,0 +1,108 @@ +using System.Reflection.Metadata.Ecma335; + +namespace Lab3 +{ + internal class Program + { + public static long methodToAnalysis1(int[] arr) + { + long n = arr.Length; + long k = n; + + for (int i = 0; i < n; i++) + { + if (arr[i] / 7 == 0) + { + k -= 2; + } + else + { + k += 3; + } + + } + + if (arr[0] > 0) + { + for (int i = 0; i < n * n; i++) + { + if (arr[0] > 0) + { + k += 3; + } + } + } + + return k; + } + + public static long methodToAnalysis2(int n, int[] arr) + { + long k = 0; + + for (int i = 0; i < n; i++) + { + k += arr[i] + FF4(i, arr); + } + + return k; + } + + public static long FF4(int n, int[] arr) + { + if (n > 0 && arr.Length > n && arr[n] < 0) + { + return FF4(n / 2, arr) + FF4(n / 3, arr); + } + + return n; + } + + public static int knapsack(int weightLimit, int n, int[] weights, int[] values) + { + if (n == 1) + { + return weights[0] <= weightLimit ? values[0] : 0; + } + if (weights[n - 1] > weightLimit) + { + return knapsack(weightLimit, n - 1, weights, values); + } + + int case1 = knapsack(weightLimit - weights[n - 1], n - 1, weights, values) + values[n - 1]; + int case2 = knapsack(weightLimit, n - 1, weights, values); + return Math.Max(case1, case2); + } + + public static int knapsackDP(int weightLimit, int n, int[] weights, int[] values) + { + int[,] memo = new int[n + 1, weightLimit +1]; + for (int i = 1; i <= n; i++) + { + for (int w = 1; w <= weightLimit; w++) + { + if (weights[i-1] <= w) + { + memo[i, w] = Math.Max(values[i - 1] + memo[i - 1, w - weights[i - 1]], memo[i - 1, w]); + } + else + { + memo[i, w] = memo[i - 1, w]; + } + } + } + + return memo[n, weightLimit]; + } + + static void Main(string[] args) + { + int weightLimit = 15; + int[] weights = new int[]{ 7, 2, 1, 9 }; + int[] values = new int[]{ 5, 4, 7, 2 }; + int n = weights.Length; + Console.WriteLine(knapsackDP(weightLimit, n, weights, values)); + Console.WriteLine(knapsack(weightLimit, n, weights, values)); + } + } +} \ No newline at end of file diff --git a/Labs.sln b/Labs.sln index 78fd219..0eb9884 100644 --- a/Labs.sln +++ b/Labs.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 17.1.32328.378 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab1", "Lab1\Lab1.csproj", "{497767A1-722A-4F48-86E5-C2AE6E0B9C9A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab2", "Lab2\Lab2.csproj", "{6AF5D775-08B9-4D4F-9E2E-116C551D0581}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab2", "Lab2\Lab2.csproj", "{6AF5D775-08B9-4D4F-9E2E-116C551D0581}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab3", "Lab3\Lab3.csproj", "{C3128C1D-C1F7-476A-808F-1A3FE5E848FA}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {6AF5D775-08B9-4D4F-9E2E-116C551D0581}.Debug|Any CPU.Build.0 = Debug|Any CPU {6AF5D775-08B9-4D4F-9E2E-116C551D0581}.Release|Any CPU.ActiveCfg = Release|Any CPU {6AF5D775-08B9-4D4F-9E2E-116C551D0581}.Release|Any CPU.Build.0 = Release|Any CPU + {C3128C1D-C1F7-476A-808F-1A3FE5E848FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C3128C1D-C1F7-476A-808F-1A3FE5E848FA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C3128C1D-C1F7-476A-808F-1A3FE5E848FA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C3128C1D-C1F7-476A-808F-1A3FE5E848FA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE