LoginSignup
0
0

More than 3 years have passed since last update.

[paiza レベルアップ問題集] 日付セット 曜日 (paizaランクB相当) youtubeの解説動画を視て修正

Last updated at Posted at 2020-02-29

一度解いたpaizaの問題youtubeの解説動画を視て修正。

namespace DayOfWeek2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int year = int.Parse(input[0]);
            int month = int.Parse(input[1]);
            int day = int.Parse(input[2]);
            int[] date = new int[] { 1800, 1, 1 };
            int count = 0;
            while(date[0] != year || date[1] != month || date[2] != day)
            {
                //一日進める
                date = nextDate(date[0], date[1], date[2]);
                count++;
            }
            string[] dayOfWeek = new string[] { "水", "木", "金", "土", "日", "月", "火" };

            Console.WriteLine(dayOfWeek[count % 7] + "曜日");
        }
        //うるう年か判断するメソッド
        static bool isLeap(int year)
        {
            return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
        }
        //ある年月の最終日を計算
        static int lastDay(int year, int month)
        {
            switch(month)
            {
                case 2:
                    if (isLeap(year))
                    {
                        return 29;
                    }
                    else
                    {
                        return 28;
                    }
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                default:
                    return 31;
            }
        }
        //翌日の年月日を返す
        static int[] nextDate(int year,int month, int day)
        {
            day++;
            if(day > lastDay(year, month))
            {
                day = 1;
                month++;
            }
            if(month > 12)
            {
                month = 1;
                year++;
            }
            return new int[] { year, month, day };
        }
    }
}

0
0
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
0
0