project-euler

https://projecteuler.net/
Log | Files | Refs | README

Euler_19.cpp (652B)


      1 #include "Euler.h"
      2 
      3 int daysInFebruary(int year)
      4 {
      5     return (year % 4 == 0) ? ((year % 100 == 0) ? ((year % 400 == 0) ? 29 : 28) : 29) : 28;
      6 }
      7 
      8 int Euler::SundayCount()
      9 {
     10     int weekday = 1;
     11     int sundays = 0;
     12 
     13     for (int year = 1900; year <= 2000; ++year)
     14     {
     15         int months[] = {31, daysInFebruary(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
     16 
     17         for (int month : months)
     18         {
     19             for (int day = 0; day < month; ++day)
     20             {
     21                 if ((weekday == 0) && (day == 0) && (year > 1900))
     22                     ++sundays;
     23 
     24                 ++weekday %= 7;
     25             }
     26         }
     27     }
     28 
     29     return sundays;
     30 }