LoginSignup
0
0

More than 3 years have passed since last update.

13日の金曜日(4桁) (paizaランク C 相当)

Posted at

問題文
YouTube解説動画


    class Program
    {
        static void Main(string[] args)
        {
            DateTime date = new DateTime(2020,3,13);
            DateTime endDate = date.AddYears(400 * 7);
            int all = 0;
            int friday13 = 0;
            while(date.CompareTo(endDate) == -1) //-1で以前、0で同じ、1で以後
            {
                if (date.Day == 13 && date.DayOfWeek.Equals(DayOfWeek.Friday)){
                    friday13++;
                }
                date = date.AddMonths(1);
                all++;
            }
            double p = (double)friday13 / all;
            double result = ((int)(p * 10000)) / 10000.0;
            Console.WriteLine(result);
        }
    }
メモ

日付の比較はDatetime構造体のCompareToメソッドを使う。
戻り値はint型で引数より前で-1、同じで0、より後で1を返す。

日付の加算はDatetime構造体のAddMonthsAddDaysメソッドを使う

0
0
2

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