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
Source
HDU 2007-11 Programming Contest_WarmUp
Submit
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int check_ord_day(int year, int month)
{
int num;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
num = 31;
break;
case 2:
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return num = 29;
else
return num = 28;
break;
case 4:
case 6:
case 9:
case 11:
num = 30;
break;
}
return num;
}
int check(int year, int month, int day)
{
if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > check_ord_day(year, month))
return 1;
}
int week_change(int year, int month, int day)
{
int s = 0;
if (month == 1) { month = 13; year--; }
if (month == 2) { month = 14; year--; }
s = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
return s;
}
char* s_change(int s)
{
char *str;
switch (s)
{
case 0:
str = "Monday";
break;
case 1:
str = "Tuesday";
break;
case 2:
str = "Wednesday";
break;
case 3:
str = "Thursday";
break;
case 4:
str = "Friday";
break;
case 5:
str = "Saturday";
break;
case 6:
str = "Sunday";
break;
}
return str;
}
int main()
{
int year, month, day;
while (scanf("%d%d%d", &year, &month, &day) != EOF)
{
if (check(year, month, day)==1)
{
printf("illegal\n");
}
else
{
int s;
s = week_change(year, month, day);
char *ss;
ss = s_change(s);
printf("%s\n",ss);
}
}
return 0;
}
Wrong Answer
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int d[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
char c[7][10]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int check(int year,int month,int day)
{
int i;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
i=1;
else i=0;
if(year<=0||month<=0||month>12||day<=0||day > d[i][month])
return 1;
}
int week_change(int year,int month,int day)
{
int s = 0;
if (month == 1) { month = 13; year--; }
if (month == 2) { month = 14; year--; }
s = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
return s;
}
int main()
{
int year,month,day;
while(scanf("%d%d%d", &year, &month, &day) != EOF)
{
if(check(year,month,day)==1)
puts("illegal");
else
{
int s;
s=week_change(year,month,day);
puts(c[s]);
}
}
return 0;
}