1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

月末最後の営業日の曜日 (paizaランク B 相当)

Posted at

問題文
YouTube解説動画

        static void Main(string[] args)
        {
            int X = int.Parse(Console.ReadLine().Trim(' '));
            int all = 0;
            int[] lastBusinessDays = new int[7]; //日、月、火、水、木、金、土
           
            DateTime date = DateTime.Now;
            DateTime endDate = date.AddYears(400 * 7);
            while(date < endDate)
            {
                DateTime lastDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
                if(lastDate.DayOfWeek == DayOfWeek.Saturday)
                {
                    lastDate = lastDate.AddDays(-1);
                }else if(lastDate.DayOfWeek == DayOfWeek.Sunday)
                {
                    lastDate = lastDate.AddDays(-2);
                }
                DayOfWeek dow = lastDate.DayOfWeek;
                switch (dow)
                {
                    case DayOfWeek.Monday:
                        lastBusinessDays[1]++;
                        break;
                    case DayOfWeek.Tuesday:
                        lastBusinessDays[2]++;
                        break;
                    case DayOfWeek.Wednesday:
                        lastBusinessDays[3]++;
                        break;
                    case DayOfWeek.Thursday:
                        lastBusinessDays[4]++;
                        break;
                    case DayOfWeek.Friday:
                        lastBusinessDays[5]++;
                        break;
                }
                date = date.AddMonths(1);
                all++;
            }
            double result = (int)(((double)lastBusinessDays[X] / all) * 1000000 ) / 1000000.0;
            Console.WriteLine(result);
        }
メモ

月末最終日は年月の日数をDateTime.DaysInMonth(year,month)メソッドで求め、DateTimeインスタンスをつくる。

int lastDay = DateTime.DaysInMonth(year,mont)
DateTime day = new DateTime(year,month,lastDay)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?