Chapter CBT Guide - Getting the classic - self-date problem

Date of issue categories:

1. basic questions:

Number of days difference between two dates request, i.e. to find the length of two specific date interval bounded date.

2. routines:

Section of the original unified range of questions to determine the starting point up (such as the difference between January 1, 0000 the date). Such data processing may be performed on the data pre-processing, on a number of days difference between pretreatment all saved date and the date of origin before the input, when input data is actually started, only O (1) time complexity saved to read is an important means of space for time.

3. Points to note - leap year:

Every leap year, February will have 29 days. Leap year judging - then the number is not divisible by 4 if it was a leap year, is divisible by 400, or 100 leap year divisible. I.e. Year% 100! = 0 && Year% 4 == 0 || Year% 400 == 0.

Example 2.3 Date difference

AC Code:

#include<cstdio>
using namespace std;

bool LeapYear(int x)
{
    return (x % 100 != 0 && x % 4 == 0) || x % 400 == 0;
}

int dayOfMonth[13][2] = { 0,0,31,31,28,29,31,31,30,30,31,31, 30 , 30 , 31 is , 31 is , 31 is , 31 is , 30 , 30 , 31 is , 31 is , 30 , 30 , 31 is , 31 is }; 

struct a Date 
{ 
    int Day;
     int Month;
     int Year;
     void nextDay () // calculated by the following day's date 
    { 
        day ++ ;
         IF (day> the dayOfMonth [Month] [LeapYear (Year)]) 
        {
            Day = . 1 ; 
            Month ++ ;
             IF (Month> 12 is ) 
            { 
                Month = . 1 ; 
                Year ++ ; 
            } 
        } 
    } 
}; 

int Days [ 5001 ] [ 13 is ] [ 32 ]; // number of days to be processed stored 

int Abs ( int X) // absolute value 
{
     return X < 0 -? X: X; 
}

int main () 
{ 
    a Date tmp; 
    int cnt = 0 ; 
    tmp.Day = 1 ; 
    tmp.Month = 1 ; 
    tmp.Year = 0 ; // initialize the object on 1 January 0000 
    the while (tmp.Year =! 5001 ) // pretreatment 
    { 
        days [tmp.Year] [tmp.Month] [tmp.Day] = CNT; // the difference between the number of days and the date stored January 1, 0000 day up 
        tmp.NextDay (); // calculating the next day's date 
        CNT ++; // counter counts, each counter is incremented after a day, date interval represents the origin and add a day 
    }
     int d1, m1, y1, d2, m2, y2;
    while (scanf("%4d%2d%2d", &y1, &m1, &d1) != EOF)
    {
        scanf("%4d%2d%2d", &y2, &m2, &d2);
        printf("%d\n", Abs(days[y2][m2][d2] - days[y1][m1][d1] + 1));
    }
    return 0;
}

例2.4 Day of Week

AC Code:

#include<cstdio>
#include<cstring>
using namespace std;

bool LeapYear(int x)
{
    return (x % 100 != 0 && x % 4 == 0) || x % 400 == 0;
}

int dayOfMonth[13][2] = { 0,0,31,31,28,29,31,31,30,30,31,31,30,30,31,31,31,31,30,30,31,31,30,30,31,31 };
char WeekName[7][20] = { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" };
char MonthName[13][20] = { "", "January","February","March","April","May","June","July","August","September","October","November","December" };

struct Date
{
    int Day;
    int Month;
    int Year;
    void NextDay()//计算下一天的日期
    {
        Day++;
        if (Day > dayOfMonth[Month][LeapYear(Year)])
        {
            Day = 1;
            Month++;
            if (Month > 12)
            {
                Month= . 1 ; 
                Year ++ ; 
            } 
        } 
    } 
}; 

int Days [ 5001 ] [ 13 is ] [ 32 ]; // number of days to be processed 

int Abs ( int X) // absolute value 
{
     return X < 0 ? - X : X; 
} 

int main () 
{ 
    a Date tmp; 
    int CNT = 0 ; 
    tmp.Day = . 1 ; 
    tmp.Month = . 1 ; 
    tmp.Year = 0 ; // initialize the object 1 January 0000 
    the while (! Tmp.Year = 5001 ) // pretreatment 
    { 
        Days [tmp.Year] [tmp.Month] [tmp.Day] = cnt; // the difference between the number of days to save the date of 1 January 0000 of up 
        tmp.NextDay (); // next day's date is calculated 
        cnt ++; // counter counts, after a day of each counter is incremented to indicate the origin and date interval and add a day 
    }
     int D, m, Y;
     char S [ 20 is ];
     the while (Scanf ( " % D% S% D " !, & D, S, & Y) = the EOF) 
    { 
        for(m = . 1; M <= 12 is ; m ++ ) 
        { 
            IF (strcmp (S, the MonthName [m]) == 0 ) BREAK ; 
        } 
        int index = Days [Y] [m] [D] - Days [ 2019 ] [ . 6 ] [ 10 ]; // comparison of June 10, 2019, Monday, 
        the printf ( " % S \ n- " , WeekName [(index% . 7 + . 7 )% . 7 ]); // process where a plurality 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/yun-an/p/11028183.html