C# :: Aufgabe #179 :: Lösung #5
7 Lösungen

#179
Ist Freitag der 13. ein Unglücks- oder Glückstag?
Anfänger - C#
von hollst
- 26.06.2017 um 13:14 Uhr
Wir betrachten den Gregorianischen Kalender mit den üblichen 12 Monaten und Schaltjahren.
Schaltjahr ist, wenn die Jahreszahl durch 4 teilbar ist, mit Ausnahme der Jahre, die durch 100,
jedoch nicht durch 400 teilbar sind. Im Schaltjahr hat der Februar 29 Tage.
Unsere Zeitrechnung beginne mit dem 1. Januar 00.
Frage: Welcher Wochentag fällt
a) am häufigsten auf einen 13. des Monats und
b) welcher bzw. welche am wenigsten.
Schaltjahr ist, wenn die Jahreszahl durch 4 teilbar ist, mit Ausnahme der Jahre, die durch 100,
jedoch nicht durch 400 teilbar sind. Im Schaltjahr hat der Februar 29 Tage.
Unsere Zeitrechnung beginne mit dem 1. Januar 00.
Frage: Welcher Wochentag fällt
a) am häufigsten auf einen 13. des Monats und
b) welcher bzw. welche am wenigsten.
#5

von daniel59 (4260 Punkte)
- 09.08.2017 um 10:49 Uhr

using System; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace ConsoleFreitag13 { class Program { const int badDay = 13; static void Main(string[] args) { var days = CountDays(DateTime.MinValue, DateTime.MaxValue).OrderByDescending(a => a.Value); var maxLength = days.Max(a => a.Key.ToString().Length); foreach (var item in days) { if (item.Key.DayInMonth == badDay) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.White; } Console.WriteLine($"{item.Key.ToString().PadRight(maxLength)}: {item.Value}"); } var max = days.Where(b => b.Value == days.Where(c => c.Key.DayInMonth == badDay).Max(a => a.Value) && b.Key.DayInMonth == badDay); var min = days.Where(b => b.Value == days.Where(c => c.Key.DayInMonth == badDay).Min(a => a.Value) && b.Key.DayInMonth == badDay); Console.WriteLine(new string('-', 40)); Console.WriteLine($"Die häufigsten Wochentage, die auf einen {badDay}. eines Monats fallen sind:"); foreach (var item in max) { Console.WriteLine($"{item.Key.ToString().PadRight(maxLength)}: {item.Value}"); } Console.WriteLine($"Die seltensten Wochentage, die auf einen {badDay}. eines Monats fallen sind:"); foreach (var item in min) { Console.WriteLine($"{item.Key.ToString().PadRight(maxLength)}: {item.Value}"); } Console.ReadLine(); } static Dictionary<Day, int> CountDays(DateTime from, DateTime to) { Dictionary<Day, int> result = new Dictionary<Day, int>(); for (DateTime dt = from; !dt.Date.Equals(to.Date); dt = dt.AddDays(1)) { Day day = new Day() { DayInMonth = dt.Day, DayInWeek = dt.DayOfWeek }; if (!result.ContainsKey(day)) { result.Add(day, 0); } result[day]++; } return result; } } class Day { static readonly CultureInfo DE = new CultureInfo("de-DE"); public int DayInMonth { get; set; } public DayOfWeek DayInWeek { get; set; } public override bool Equals(object obj) { Day d = (Day)obj; return DayInMonth == d.DayInMonth && DayInWeek == d.DayInWeek; } public override int GetHashCode() { int hash = 13; hash = (hash * 7) + DayInMonth.GetHashCode(); hash = (hash * 7) + DayInWeek.GetHashCode(); return hash; } public override string ToString() { return $"{DE.DateTimeFormat.GetDayName(DayInWeek)} der {DayInMonth}."; } } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1