codeup 1928 period difference

Date processing is a real headache and requires great care in dealing with details. (But after looking at the solution in algorithmic notes, I think the method is the most important...)

Here is a question on codeup to introduce the relevant issues, as well as some new points of knowledge learned from it. The title is as follows:

Title Description
 There are two dates. Let's find the number of days between the two dates. If the two dates are continuous, we stipulate that the number of days between them is two days.

input
 There are multiple sets of data, each with two rows representing two dates in the form of YYYYMMDD

output
 Each group of data outputs one line, the date difference

sample input
20130101
20130105
 sample output
5

A very conventional topic proves that "detail processing is very important", but it also proves that "method is more important".

Title Analysis

First of all, analyze this topic. We need to do the following:

1. Input of processing date
 2. How to calculate the difference between two dates

Processing date input

My method

I think of using sscanf I just learned to process input, as follows:

char time1[10],time2[10];

scanf("%s",time1);
scanf("%s",time2);

int year1,month1,day1;
int year2,month2,day2;

sscanf(time1,"%4d%2d%2d",&year1,&month1,&day1);
sscanf(time2,"%4d%2d%2d",&year2,&month2,&day2);

At first glance, this method is possible, but in fact it is also possible. The purpose of getting input is simply to get the year, month and day out, and this method has achieved such a goal.

However, in order to follow the procedure, we have to divide time1 and time2 into different sizes. At first, I thought that comparing the size of time1 and time2 was very troublesome and needed a lot of branching judgments. When I write this article again, I suddenly wake up. By analogy with the following methods, we can directly use the string comparison function (strcmp function) for comparison. It is feasible to test by ourselves.

The Method of Processing Input in Algorithmic Notebook

The following gives the method of processing input in algorithmic notes. I didn't expect that before the string comparison method mentioned above, I thought this method was really ingenious. The code is as follows:

int time1, time2; //Time 1 saves hours and time 2 saves hours.
int year1,month1,day1,  year2,month2,day2;

scanf("%d%d",&time1, &time2)

    if(time1 > time2){//Guarantee that time 1 is saved for an hour and time 2 is saved for a long time.
        int temp = time1;
        time1 = time2;
        time2 = temp;
    }

    // Dividing (10 ^ (n+1)) takes the first n digits and the module takes the last n digits. 
    year1 = time1/10000; month1 = time1%10000/100; day1 = time1%100;
    year2 = time2/10000; month2 = time2%10000/100; day2 = time2%100;

It can be seen from the code that time 1 and time 2 are obtained directly by two integer variables, and then the corresponding digits are obtained by dividing and modularizing operations.

After reflection, I find that the fundamental reason why I did not think of this method is that I did not have a thorough understanding of the essence of division and modular operation. From this, I draw a conclusion:

Conclusion:
Dividing and modular operations can be used to obtain the corresponding digits of an integer, and there are the following rules:
- If you want to get the first N bit of integer X, use X/10(N+1)
- If you want to get the post-N bit of integer X, use X%10(N+1)

In this way, the two methods can be said to be the same way, the key point is to return to how to calculate the time difference of two years.

Calculate the difference between dates

My thoughts

First of all, my first idea is to input two dates, 20160328 and 101118. My idea is to divide the calculation into three parts. The total number of days is 20101-20160101, 20160328, 20110118-20111231. This method has a lot of details to consider. No surprise, I lost...

Later, the idea was to use a "marking time", such as 19700101, and then calculate the number of days x and y of the two input dates from the "marking time", respectively. The final result is x-y (or other relationship, the total value is the difference between the two distances, I believe the reader can understand). This method has not been realized, because I looked at the following methods, it is amazing, too clever! But it's just according to the actual situation.

The Method of Calculating the Difference between Two Dates in Algorithmic Notebook

As the last sentence in the previous paragraph, the method in algorithmic notes is only simulated according to the "actual situation" of time. (I think that's why Sunshine put the date issue in Chapter 3, Introduction Simulation...)

What is "the actual situation in time"? It's from knowing the date of time 1, day by day to time 2. Maybe it's not clear enough. See the code below.

int sum = 1;//The initial value is 1 instead of 0.

while(year1<year2 || month1<month2 || day1<day2){

    day1++;

    if(day1 > monthNum[month1][isLeapYear(year1)]){
        month1++;
        day1 = 1;//Attention points
    }

    if(month1 > 12){
        year1++;
        month1 = 1;//Attention points
    }

    sum++;
} 

I believe I can understand the above insights when I see the code.

Among them, isLeapYear() is a function to determine whether it is a leap year or not?
bool isLeapYear(int year){
    return ( (year%4 == 0 && year%100 != 0)  ||  (year % 400 == 0) );
}
monthNum is an array
int monthNum[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} }; 

Almost all the ingenuity of this method is embodied in the above two pieces of code. Readers should appreciate the subtlety of this method for themselves.

Complete code

#include<stdio.h>

bool isLeapYear(int year);

int monthNum[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} }; 

int main(){

    int time1, time2; //Time 1 saves hours and time 2 saves hours.
    int year1,month1,day1,  year2,month2,day2;

    while(scanf("%d%d",&time1, &time2) != EOF) {

        if(time1 > time2){
            int temp = time1;
            time1 = time2;
            time2 = temp;
        }

        // Dividing (10 ^ (x+1)) takes the front x-digit and the module takes the back x-digit. 
        year1 = time1/10000; month1 = time1%10000/100; day1 = time1%100;
        year2 = time2/10000; month2 = time2%10000/100; day2 = time2%100;

        int sum = 1;

        while(year1<year2 || month1<month2 || day1<day2){

            day1++;

            if(day1 > monthNum[month1][isLeapYear(year1)]){
                month1++;
                day1 = 1;
            }

            if(month1 > 12){
                year1++;
                month1 = 1;
            }

            sum++;
        } 

        printf("%d\n",sum);
    }

    return 0;
}

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

attach

Algorithmic notes Purchase address.

Posted by summerpewp on Sat, 23 Mar 2019 09:54:53 -0700