1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
using namespace std;
//======================================================================
bool isLeap( int year )
{
if ( year % 400 == 0 ) return true;
if ( year % 100 == 0 ) return false;
return ( year % 4 == 0 );
}
//======================================================================
int main()
{
const int MONTHLENGTH[ 1 + 12 ] = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Includes Dec from previous year, so as to use [i-1] for previous month length
// Also allows Jan = month 1, Feb = month 2 etc.
const char *names[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
int tally[7] = { 0 };
int day = 1; // cumulative days from first of 1900 (a Monday)
for ( int year = 1900; year <= 2000; year++ )
{
for ( int month = 1; month <= 12; month++ )
{
if ( !(year == 1900 && month == 1) ) day += MONTHLENGTH[month-1]; // cumulative days at 1st of month
if ( month == 3 && isLeap( year ) ) day++; // 1st of March in a leap year
if ( year >= 1901 ) tally[day%7]++; // count from 1901 onward
}
}
for ( int i = 0; i < 7; i++ ) cout << names[i] << '\t' << tally[i] << '\n';
}
|