C# :: Aufgabe #179

5 Lösungen Lösungen öffentlich

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.

Lösungen:

vote_ok
von Mexx (2190 Punkte) - 26.06.2017 um 22:48 Uhr
Der Freitag kommt mit 3469x am häufigsten vor.

Monday: 3452
Tuesday: 3455
Wednesday: 3462
Thursday: 3448
Friday: 3469
Saturday: 3448
Sunday: 3464

Quellcode ausblenden 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();
        }
    }
}
vote_ok
von hollst (6440 Punkte) - 29.06.2017 um 08:42 Uhr
Quellcode ausblenden 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;
        }
    }
}
vote_ok
von kjaenke (410 Punkte) - 30.06.2017 um 13:11 Uhr
Quellcode ausblenden 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; }
    }
vote_ok
von drumler (40 Punkte) - 27.07.2017 um 09:35 Uhr
Quellcode ausblenden 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();
        }
    }
}
vote_ok
von daniel59 (2700 Punkte) - 09.08.2017 um 10:49 Uhr
Quellcode ausblenden 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}.";
        }
    }
}