Commit 0e90a829 by Andrew Dahl

Solved Problem 19

parent 9648abdd
Showing with 62 additions and 0 deletions
Question:
You are given the following information, but you may prefer to do some research for yourself.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
Answer: 171
#include <iostream>
using namespace std;
int sundays = 0;
void check(int days)
{
if(days % 7 == 0)
sundays++;
}
int main()
{
int days = 366;
for(int year = 1901; year <= 2000; year++)
{
check(days);
days += 31; //January
check(days);
days += (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28); //February
check(days);
days += 31; //March
check(days);
days += 30; //April
check(days);
days += 31; //May
check(days);
days += 30; //June
check(days);
days += 31; //July
check(days);
days += 31; //August
check(days);
days += 30; //September
check(days);
days += 31; //October
check(days);
days += 30; //November
check(days);
days += 31; //December
}
cout << sundays << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment