Description
Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?
Input
There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).
Output
Output one line.
if the date is illegal, you should output "illegal". Or, you should output what day it is.
Sample Input
2007 11 17
Sample Output
Saturday
#include<iostream>
#include<string>
using namespace std;
bool leapYear(int n) {
if ((n % 100 != 0 && n % 4 == 0) || (n % 400 == 0)) {
return true;
}
return false;
}
string date(int n) {
if (n == 0) {
return "Saturday";
}
if (n == 1 || n == -6) {
return "Sunday";
}
if (n == 2 || n == -5) {
return "Monday";
}
if (n == 3 || n == -4) {
return "Tuesday";
}
if (n == 4 || n == -3) {
return "Wednesday";
}
if (n == 5 || n == -2) {
return "Thursday";
}
if (n == 6 || n == -1) {
return "Friday";
}
}
int Month(int n, int year) {
if (n == 1 || n == 3 || n == 5 || n == 7 || n == 8 || n == 10 || n == 12) {
return 31;
}
if (n == 2 && leapYear(year) == true) {
return 29;
}
if (n == 2 && leapYear(year) == false) {
return 28;
}
return 30;
}
bool correct(int y, int m, int n) {
if (y>10000||y<0)
{
return false;
}
if (m > 12 || m <= 0) {
return false;
}
if (n > 31 || n <= 0) {
return false;
}
if ((m == 4 || m == 6 || m == 9 || m == 11) && n>30) {
return false;
}
if (m == 2 && leapYear(y) == true && n > 29) {
return false;
}
if (m == 2 && leapYear(y) == false && n > 28) {
return false;
}
return true;
}
int main() {
int year, month, day;
while (cin>>year>>month>>day)
{
int sum = 0;
if (correct(year, month, day) == false) {
cout << "illegal" << endl;
}
else {
if (year == 2007) {
if (month == 11 && day >= 17) {
sum = day - 17;
}
if (month == 11 && day < 17) {
sum = day - 17;
}
if (month>11) {
sum = 13 + day;
}
if (month < 11) {
for (int s = month + 1;s < 11;s++) {
sum -= Month(s, year);
}
sum = sum - (Month(month, year) - day);
sum -= 17;
}
int n = sum % 7;
cout << date(n) << endl;
}
if (year>2007) {
for (int i = 2008;i < year;i++) {
if (leapYear(i)) {
sum += 366;
}
else {
sum += 365;
}
}
for (int j = 1;j < month;j++) {
sum += Month(j, year);
}
sum = sum + 44 + day;
int n = sum % 7;
cout << date(n) << endl;
}
if (year < 2007) {
sum = -314;
for (int r = year + 1;r < 2007;r++) {
if (leapYear(r)) {
sum -= 366;
}
else {
sum -= 365;
}
}
for (int s = month + 1;s <= 12;s++) {
sum -= Month(s, year);
}
sum = sum - (Month(month, year) - day);
int n = sum % 7;
cout << date(n) << endl;
}
}
}
return 0;
}