Algorithm to improve date calculation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.