From cf5fc376d59326ac046137731a91c0b8ac48c88d Mon Sep 17 00:00:00 2001 From: Jens Albrecht <jens.albrecht@th-nuernberg.de> Date: Sun, 1 Dec 2024 21:52:33 +0100 Subject: [PATCH] . --- Einheitsmatrix/Einheitsmatrix.cs | 54 ++++++++++++++++++++++ Einheitsmatrix/Einheitsmatrix.csproj | 10 +++++ Felder2D/Felder2D.cs | 67 ++++++++++++++++++++++++++++ Felder2D/Felder2D.csproj | 10 +++++ ProjectEuler11/ProjectEuler11.cs | 60 +++++++++++++++++++++++++ ProjectEuler11/ProjectEuler11.csproj | 10 +++++ Uebung08.sln | 43 ++++++++++++++++++ WerteMatrix/WerteMatrix.cs | 28 ++++++++++++ WerteMatrix/WerteMatrix.csproj | 10 +++++ 9 files changed, 292 insertions(+) create mode 100644 Einheitsmatrix/Einheitsmatrix.cs create mode 100644 Einheitsmatrix/Einheitsmatrix.csproj create mode 100644 Felder2D/Felder2D.cs create mode 100644 Felder2D/Felder2D.csproj create mode 100644 ProjectEuler11/ProjectEuler11.cs create mode 100644 ProjectEuler11/ProjectEuler11.csproj create mode 100644 Uebung08.sln create mode 100644 WerteMatrix/WerteMatrix.cs create mode 100644 WerteMatrix/WerteMatrix.csproj diff --git a/Einheitsmatrix/Einheitsmatrix.cs b/Einheitsmatrix/Einheitsmatrix.cs new file mode 100644 index 0000000..f645977 --- /dev/null +++ b/Einheitsmatrix/Einheitsmatrix.cs @@ -0,0 +1,54 @@ +class Program +{ + // Prüft, ob m eine Einheitsmatrix ist + static bool IsUnity(int[,] m) + { + // Prüfe, ob quadratisch + if (m == null || m.GetLength(0) != m.GetLength(1)) + return false; + + for (int i = 0; i < m.GetLength(0); i++) + { + for (int j = 0; j < m.GetLength(1); j++) + { + if (i == j) + { + if (m[i, j] != 1) // Prüfe Diagonale + return false; + } + else + { + if (m[i, j] != 0) // alle anderen Werte + return false; + } + } + } + + return true; + } + + // Hauptprogramm zum Testen + static void Main() + { + int[,] m1 = { { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, 0, 1 } }; + + int[,] m2 = { { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, 1, 1 } }; + + int[,] m3 = { { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, 0, 0 } }; + + int[,] m4 = { { 1, 0, 0 }, + { 0, 2, 0 }, + { 0, 0, 1 } }; + + Console.WriteLine($"IsUnity(m1) = {IsUnity(m1)}"); + Console.WriteLine($"IsUnity(m2) = {IsUnity(m2)}"); + Console.WriteLine($"IsUnity(m3) = {IsUnity(m3)}"); + Console.WriteLine($"IsUnity(m4) = {IsUnity(m4)}"); + } +} diff --git a/Einheitsmatrix/Einheitsmatrix.csproj b/Einheitsmatrix/Einheitsmatrix.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Einheitsmatrix/Einheitsmatrix.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Felder2D/Felder2D.cs b/Felder2D/Felder2D.cs new file mode 100644 index 0000000..bf16bb6 --- /dev/null +++ b/Felder2D/Felder2D.cs @@ -0,0 +1,67 @@ +class Program +{ + // Gibt Feld aus + // Parameter mitSumme steuert, ob eine Summen-Zeile erzeugt werden soll + static void Ausgabe(int[,] f, bool mitSumme = false) + { + for (int i = 0; i < f.GetLength(0); i++) + { + for (int j = 0; j < f.GetLength(1); j++) + Console.Write($"{f[i, j],5}"); + Console.WriteLine(); + } + + if (mitSumme) + { + for (int j = 0; j < f.GetLength(1); j++) + Console.Write("-----"); + Console.WriteLine(); + + // für jede Spalte, berechne die Summe der Zeilen + for (int j = 0; j < f.GetLength(1); j++) + { + int summe = 0; + for (int i = 0; i < f.GetLength(0); i++) + summe += f[i, j]; + Console.Write($"{summe,5}"); + } + Console.WriteLine(); + } + } + + // Transponiert eine Matrix + static int[,] Transponiere(int[,] f) + { + int[,] ftrans = new int[f.GetLength(1), f.GetLength(0)]; + + for (int i = 0; i < ftrans.GetLength(0); i++) + for (int j = 0; j < ftrans.GetLength(1); j++) + ftrans[i, j] = f[j, i]; + + return ftrans; + } + + static void Main(string[] args) + { + Random rnd = new Random(); + + // Feld anlegen und befüllen + int[,] f1 = new int[3, 5]; + for (int i = 0; i < f1.GetLength(0); i++) + for (int j = 0; j < f1.GetLength(1); j++) + f1[i, j] = rnd.Next(10); + + // Feld ausgeben + Console.WriteLine("Original:"); + Ausgabe(f1); + + // neues, transponiertes Feld erzeugen und ausgeben + int[,] f2 = Transponiere(f1); + Console.WriteLine("\nTransponiert:"); + Ausgabe(f2); + + // Ausgabe mit Summen-Zeile + Console.WriteLine("\nMit Summe:"); + Ausgabe(f2, true); + } +} diff --git a/Felder2D/Felder2D.csproj b/Felder2D/Felder2D.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Felder2D/Felder2D.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/ProjectEuler11/ProjectEuler11.cs b/ProjectEuler11/ProjectEuler11.cs new file mode 100644 index 0000000..cccde3b --- /dev/null +++ b/ProjectEuler11/ProjectEuler11.cs @@ -0,0 +1,60 @@ +int[,] m = {{08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08}, + {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00}, + {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65}, + {52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91}, + {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80}, + {24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50}, + {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70}, + {67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21}, + {24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72}, + {21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95}, + {78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 09, 53, 56, 92}, + {16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57}, + {86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58}, + {19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40}, + {04, 52, 08, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66}, + {88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69}, + {04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 08, 46, 29, 32, 40, 62, 76, 36}, + {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16}, + {20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54}, + {01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}}; + +int n = 4; +int max = m[0, 0]; + +int rows = m.GetLength(0); +int cols = m.GetLength(1); + +for (int i = 0; i < rows; i++) +{ + for (int j = 0; j < cols; j++) + { + int prod = 1; + // check right + for (int k = 0; k < n && j + k < cols; k++) + prod *= m[i, j + k]; + max = prod > max ? prod : max; + + // check down + prod = 1; + for (int k = 0; k < n && i + k < rows; k++) + prod *= m[i+k, j]; + max = prod > max ? prod : max; + + // check diagonal down right + prod = 1; + for (int k = 0; k < n && i + k < rows && j + k < cols; k++) + prod *= m[i + k, j + k]; + max = prod > max ? prod : max; + + // check diagonal up right + prod = 1; + for (int k = 0; k < n && i - k >= 0 && j + k < cols; k++) + prod *= m[i - k, j + k]; + max = prod > max ? prod : max; + } +} + +Console.WriteLine($"Max: {max}"); + + diff --git a/ProjectEuler11/ProjectEuler11.csproj b/ProjectEuler11/ProjectEuler11.csproj new file mode 100644 index 0000000..d439800 --- /dev/null +++ b/ProjectEuler11/ProjectEuler11.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net7.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Uebung08.sln b/Uebung08.sln new file mode 100644 index 0000000..21e0f4b --- /dev/null +++ b/Uebung08.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Einheitsmatrix", "Einheitsmatrix\Einheitsmatrix.csproj", "{9B11E37E-71A8-4517-BE59-C7F0D8822139}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WerteMatrix", "WerteMatrix\WerteMatrix.csproj", "{AEF18FB4-9BF4-4B4F-B5FE-6A71899A79CB}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Felder2D", "Felder2D\Felder2D.csproj", "{08866800-538C-480D-A272-CFE8044DD966}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectEuler11", "ProjectEuler11\ProjectEuler11.csproj", "{C82B72E1-7C3B-47AF-BE5E-37CCF9FC06EF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9B11E37E-71A8-4517-BE59-C7F0D8822139}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9B11E37E-71A8-4517-BE59-C7F0D8822139}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9B11E37E-71A8-4517-BE59-C7F0D8822139}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9B11E37E-71A8-4517-BE59-C7F0D8822139}.Release|Any CPU.Build.0 = Release|Any CPU + {AEF18FB4-9BF4-4B4F-B5FE-6A71899A79CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AEF18FB4-9BF4-4B4F-B5FE-6A71899A79CB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AEF18FB4-9BF4-4B4F-B5FE-6A71899A79CB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AEF18FB4-9BF4-4B4F-B5FE-6A71899A79CB}.Release|Any CPU.Build.0 = Release|Any CPU + {08866800-538C-480D-A272-CFE8044DD966}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {08866800-538C-480D-A272-CFE8044DD966}.Debug|Any CPU.Build.0 = Debug|Any CPU + {08866800-538C-480D-A272-CFE8044DD966}.Release|Any CPU.ActiveCfg = Release|Any CPU + {08866800-538C-480D-A272-CFE8044DD966}.Release|Any CPU.Build.0 = Release|Any CPU + {C82B72E1-7C3B-47AF-BE5E-37CCF9FC06EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C82B72E1-7C3B-47AF-BE5E-37CCF9FC06EF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C82B72E1-7C3B-47AF-BE5E-37CCF9FC06EF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C82B72E1-7C3B-47AF-BE5E-37CCF9FC06EF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C4BEB637-30E7-491B-908C-EC181ED94E50} + EndGlobalSection +EndGlobal diff --git a/WerteMatrix/WerteMatrix.cs b/WerteMatrix/WerteMatrix.cs new file mode 100644 index 0000000..157a6e4 --- /dev/null +++ b/WerteMatrix/WerteMatrix.cs @@ -0,0 +1,28 @@ +namespace Uebung; + +class Program +{ + static void Ausgabe(int[,] f, bool mitSumme = false) + { + for (int i = 0; i < f.GetLength(0); i++) + { + for (int j = 0; j < f.GetLength(1); j++) + Console.Write($"{f[i, j],5}"); + Console.WriteLine(); + } + } + + static void Main() + { + Console.Write("n eingeben: "); + int n = Convert.ToInt32(Console.ReadLine()); + + int[,] matrix = new int[n, n]; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + matrix[i, j] = j - i; + + Ausgabe(matrix); + } +} \ No newline at end of file diff --git a/WerteMatrix/WerteMatrix.csproj b/WerteMatrix/WerteMatrix.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/WerteMatrix/WerteMatrix.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> -- GitLab