C# :: Aufgabe #179
7 Lösungen
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.
Lösungen:
Der Freitag kommt mit 3469x am häufigsten vor.
Monday: 3452
Tuesday: 3455
Wednesday: 3462
Thursday: 3448
Friday: 3469
Saturday: 3448
Sunday: 3464
Monday: 3452
Tuesday: 3455
Wednesday: 3462
Thursday: 3448
Friday: 3469
Saturday: 3448
Sunday: 3464
C#-Code
using System; using System.Collections; using System.Collections.Generic; namespace Freitag_der_13_Tagesbestimmung { class Program { static void Main(string[] args) { DateTime dtBeginn = new DateTime(0); DateTime dtNow = DateTime.Now; List<object[]> matches = new List<object[]>(); Hashtable htAnzahl = new Hashtable(); htAnzahl.Add(DayOfWeek.Monday.ToString(), 0); htAnzahl.Add(DayOfWeek.Tuesday.ToString(), 0); htAnzahl.Add(DayOfWeek.Wednesday.ToString(), 0); htAnzahl.Add(DayOfWeek.Thursday.ToString(), 0); htAnzahl.Add(DayOfWeek.Friday.ToString(), 0); htAnzahl.Add(DayOfWeek.Saturday.ToString(), 0); htAnzahl.Add(DayOfWeek.Sunday.ToString(), 0); while (dtBeginn < dtNow) { if (dtBeginn.Day == 13) matches.Add(new object[] { dtBeginn.Day, dtBeginn.Month, dtBeginn.Year, dtBeginn.DayOfWeek }); dtBeginn = dtBeginn.AddDays(1); } matches.ForEach(x => { //Console.WriteLine(string.Format(x[0].ToString() + "." + x[1].ToString() + "." + x[2].ToString() + ":\t" + x[3].ToString())); htAnzahl[x[3].ToString()] = (int)htAnzahl[x[3].ToString()] + 1; }); Console.WriteLine(); Console.WriteLine(DayOfWeek.Monday.ToString() + ": \t" + htAnzahl[DayOfWeek.Monday.ToString()].ToString()); Console.WriteLine(DayOfWeek.Tuesday.ToString() + ":\t" + htAnzahl[DayOfWeek.Tuesday.ToString()].ToString()); Console.WriteLine(DayOfWeek.Wednesday.ToString() + ":\t" + htAnzahl[DayOfWeek.Wednesday.ToString()].ToString()); Console.WriteLine(DayOfWeek.Thursday.ToString() + ":\t" + htAnzahl[DayOfWeek.Thursday.ToString()].ToString()); Console.WriteLine(DayOfWeek.Friday.ToString() + ": \t" + htAnzahl[DayOfWeek.Friday.ToString()].ToString()); Console.WriteLine(DayOfWeek.Saturday.ToString() + ":\t" + htAnzahl[DayOfWeek.Saturday.ToString()].ToString()); Console.WriteLine(DayOfWeek.Sunday.ToString() + ": \t" + htAnzahl[DayOfWeek.Sunday.ToString()].ToString()); Console.ReadKey(); } } }
C#-Code
using System; using static System.Console; namespace freitag13 { class Program { static void Main() { int startjahr = 1, endjahr = 2017, startmonat = 1, endmonat = 12; int[] verteilung = new int[7]; for (int jahr = startjahr; jahr <= endjahr; jahr++) for(int monat = startmonat; monat <= endmonat; monat++) { DateTime value = new DateTime(jahr, monat, 13); verteilung[(int)value.DayOfWeek]++; } for (var i = 0; i < verteilung.Length; i++) WriteLine(i.ToString() + ": " + Enum.GetName(typeof(DayOfWeek), i).ToMyString(10) + " " + verteilung[i].ToString("n0")); ReadKey(true); } } static class MyExtensions { public static string ToMyString<T>(this T s, int chars) { string result = s.ToString(); while (result.Length < chars) result += " "; return result; } } }
C#-Code
internal static class Program { private static void Main() { DateTime day = new DateTime(1, 1, 1); Time time = new Time { Monday = 0, Tuesday = 0, Wednesday = 0, Thursday = 0, Friday = 0, Saturday = 0, Sunday = 0 }; while (day.ToLongDateString() != DateTime.Now.ToLongDateString()) { if (day.Day == 13) { if (day.DayOfWeek == DayOfWeek.Monday) { time.Monday += 1; } if (day.DayOfWeek == DayOfWeek.Tuesday) { time.Tuesday += 1; } if (day.DayOfWeek == DayOfWeek.Wednesday) { time.Wednesday += 1; } if (day.DayOfWeek == DayOfWeek.Thursday) { time.Thursday += 1; } if (day.DayOfWeek == DayOfWeek.Friday) { time.Friday += 1; } if (day.DayOfWeek == DayOfWeek.Saturday) { time.Saturday += 1; } if (day.DayOfWeek == DayOfWeek.Sunday) { time.Sunday += 1; } } Debug.WriteLine(day.ToShortDateString()); day = day.AddDays(1); } Console.WriteLine("Seit unserer Zeitrechnung gab es:"); Console.WriteLine($"{time.Monday} Montag den 13."); Console.WriteLine($"{time.Tuesday} Dienstag den 13."); Console.WriteLine($"{time.Wednesday} Mittwoch den 13."); Console.WriteLine($"{time.Thursday} Donnerstag den 13."); Console.WriteLine($"{time.Friday} Freitag den 13."); Console.WriteLine($"{time.Saturday} Samstag den 13."); Console.WriteLine($"{time.Sunday} Sonntag den 13."); Console.Read(); } } internal class Time { public int Monday { get; set; } public int Tuesday { get; set; } public int Wednesday { get; set; } public int Thursday { get; set; } public int Friday { get; set; } public int Saturday { get; set; } public int Sunday { get; set; } }
C#-Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { int monday = 0; int tuesday = 0; int wednesday = 0; int thursday = 0; int friday = 0; int saturday = 0; int sunday = 0; DateTime today = DateTime.Now; DateTime newday = new DateTime(0001,1,1); TimeSpan one_day = new TimeSpan(24,0,0); while (newday.ToShortDateString() != today.ToShortDateString()) { newday = newday + one_day; if (newday.Day == 13) { switch (newday.DayOfWeek.ToString().ToLower()) { case "monday": monday++; break; case "tuesday": tuesday++; break; case "wednesday": wednesday++; break; case "thursday": thursday++; break; case "friday": friday++; break; case "saturday": saturday++; break; case "sunday": sunday++; break; } } } Console.WriteLine("Monday: {0} \n Tuesday: {1} \n Wednesday: {2} \n Thursday: {3} \n Friday: {4} \n Saturday: {5} \n Sunday: {6}", monday.ToString(), tuesday.ToString(), wednesday.ToString(),thursday.ToString(),friday.ToString(),saturday.ToString(),sunday.ToString()); Console.ReadLine(); } } }
C#-Code
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}."; } } }
C#-Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace der13 { class Program { static int[] weekDayCounter = new int[7]; static int counter; static string weekdayMax; static string weekdayMin; static int day; static int daysave; static float i = 1; static float numbersave; static bool switchYear; static void Main(string[] args) { Render(); Console.WriteLine(Convert.ToString(weekDayCounter.Max())); if (weekDayCounter.ToList().IndexOf(weekDayCounter.Max()) == 0) { weekdayMax = "Monday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Max()) == 1) { weekdayMax = "Tuesday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Max()) == 2) { weekdayMax = "Wednesday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Max()) == 3) { weekdayMax = "Thursday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Max()) == 4) { weekdayMax = "Friday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Max()) == 5) { weekdayMax = "Saturday "; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Max()) == 6) { weekdayMax = "Sunday "; } Console.WriteLine("The day with the most 13th. is the {0}.", weekdayMax); if (weekDayCounter.ToList().IndexOf(weekDayCounter.Min()) == 0) { weekdayMin = "Monday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Min()) == 1) { weekdayMin = "Tuesday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Min()) == 2) { weekdayMin = "Wednesday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Min()) == 3) { weekdayMin = "Thursday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Min()) == 4) { weekdayMin = "Friday"; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Min()) == 5) { weekdayMin = "Saturday "; } else if (weekDayCounter.ToList().IndexOf(weekDayCounter.Min()) == 6) { weekdayMin = "Sunday "; } Console.WriteLine("The day with the less 13th. is the {0}.", weekdayMin); Console.ReadKey(); } private static void Render() { for (i = 1; i <= 2018; i++) { Schaltjahr(); if (i <= 2018 & switchYear == true) { Tag31(); Tag29(); Tag31(); Tag30(); Tag31(); Tag30(); Tag31(); Tag31(); Tag30(); Tag31(); Tag30(); Tag31(); } if (i <= 2018 & switchYear == false) { Tag31(); Tag28(); Tag31(); Tag30(); Tag31(); Tag30(); Tag31(); Tag31(); Tag30(); Tag31(); Tag30(); Tag31(); } } } private static void Schaltjahr() { switchYear = true; numbersave = i / 100; if (numbersave == (int)numbersave) { switchYear = false; } numbersave = i / 400; if (numbersave == (int)numbersave) { switchYear = true; numbersave = i / 4; if (numbersave != (int)numbersave & switchYear != false) { switchYear = false; } else { switchYear = true; } } } private static void Tag28() { while (day <= 7) { Provider(); if (counter == 28) { counter = 0; break; } } } private static void Tag29() { while (day <= 7) { Provider(); if (counter == 29) { counter = 0; break; } } } private static void Tag31() { while(day <= 7) { Provider(); if (counter == 31) { counter = 0; break; } } } private static void Tag30() { while (day <= 7) { Provider(); if (counter == 30) { counter = 0; break; } } } private static void Tagsaver() { if (daysave == 1) { weekDayCounter[0]++; } if (daysave == 2) { weekDayCounter[1]++; } if (daysave == 3) { weekDayCounter[2]++; } if (daysave == 4) { weekDayCounter[3]++; } if (daysave == 5) { weekDayCounter[4]++; } if (daysave == 6) { weekDayCounter[5]++; } if (daysave == 7) { weekDayCounter[6]++; } } private static void Provider() { counter++; day++; if (counter == 13) { daysave = day; Tagsaver(); } if (day == 7) { day = 0; } } } }
NET 5.x; C# 9.x
Neuerung in C# 9.x: Top-Level-Anweisung
https://docs.microsoft.com/de-de/dotnet/csharp/whats-new/csharp-9
Neuerung in C# 9.x: Top-Level-Anweisung
https://docs.microsoft.com/de-de/dotnet/csharp/whats-new/csharp-9
C#-Code
using System; using System.Linq; var sDate = new DateTime(1, 1, 13); var eDate = new DateTime(2020, 12, 13); var arrWeekdays = new int[7]; while(sDate <= eDate) { arrWeekdays[(int)sDate.DayOfWeek]++; sDate = sDate.AddMonths(1); } var lst = Enumerable.Range(0, arrWeekdays.Length).Select(x => (wd: Enum.GetName(typeof(DayOfWeek), x), count: arrWeekdays[x])).ToList(); lst.ForEach(x => Console.WriteLine($"{x.wd}: {x.count}")); Console.WriteLine($"\nMax: {lst.OrderBy(x => -x.count).FirstOrDefault()}"); Console.WriteLine($"\nMin: {lst.OrderBy(x => x.count).FirstOrDefault()}");