Algorithm increase date Calculation time limit: 1.0s memory Limit: 256.0MB problem description known November 11, 2011 is Friday, ask yyyy mm month DD is the day of the week? Note Consider the case of a leap year. In particular, every hundred years do not leap, the 400-year leap in the situation. Input format input only one line
YYYY MM DD output format output only one line
W Data size and conventions 1599 <= YYYY <= 2999
1 <= MM <= 12
1 <= DD <= 31, and ensure that the test sample yyyy mm month DD Day is a reasonable date
1 <= W <= 7, respectively, representing Monday to Sunday example input 2011 11 11 Sample Output 5 dozen tables
#include <cstdio> #include <cstdlib> #include <cstring>using namespace Std;int week[3005][15][35]; int Month1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int month2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; void count () {int Year=2011,month=11,day=11,ans=5;week[year][month][day]=5;while (1) {if (year==3000) break;if (year% 400==0| | (year%4==0&&year%100!=0)) {day++;if (Day>month2[month]) {month++;d ay=1;} if (month>12) {month=1;year++;} Ans++;if (ans>7) Ans=1;week[year][month][day]=ans;} else {day++;if (Day>month1[month]) {month++;d ay=1;} if (month>12) {month=1;year++;} Ans++;if (ans>7) Ans=1;week[year][month][day]=ans;}} Year=2011;month=11;day=11;ans=5;while (1) {if (year==1598) break;if (year%400==0| | (year%4==0&&year%100!=0)) {day--;if (day<=0) {month--;if (month<=0) Day=31;else day=month2[month];} if (month<=0) {month=12;year--;} Ans--;if (ans<=0) Ans=7;week[year][month][day]=ans;} else {day--;if (day<=0) {month--;if (month<=0) Day=31;else day=month1[month];} if (month<=0) {month=12;year--;} Ans--;if (ans<=0) Ans=7;week[year][month][day]=ans;}} int main () {count (); int Y,m,d;while (~SCANF ("%d%d%d", &y,&m,&d)) printf ("%d\n", Week[y][m][d]); return 0;}
Algorithm to improve date calculation