T1175 calculates the number of days between two dates

Author: Corrugated flying fish

T1175 calculates the number of days between two dates

Noriaki porridge rolled out the industry, from the shop to be a hundred years old as well as 36521 days!
There will always be a few memorable day. So, how do we calculate the number of days between two dates it?

topic

Here Insert Picture Description
At first glance the title, can not help but make people question issued disdain in their hearts:? "It would only need to subtract two dates and then output the results.
Obviously we did before entry and similar topics, what can such a simple topic as the popularity of T2? "

Problem-solving

But when we think carefully about the core purpose of this question will be found hidden in a lot of "pit."
Number of days this blog idea: first calculate the number of days between two dates from the year YuanRi January, then subtract the number of days, so the number of days between two dates.

A flowchart

The core extracted from the title, we can draw a flow chart.

Created with Raphaël 2.2.0 开始 输入两个日期 两个日期是 否在同一年? 两个日期是 否在同一月? 计算:日 输出天数 结束 计算:月,日 计算:两个日期距离 元年元月元日的天数 计算:两个天数之差 yes no yes no

Second, extract key points

Flowcharting roughly finished, but success depends on details, we need to add some key points of our processes.
1.Determine whether to include between two dates in February of a leap year, if you need to include the number of days plus 1.
2.In January, the number of days the month represented by zero. In addition to January, the number of days the representative of the rest of the month and the number of days for the month prior to the month represented.
3.The number of days the first year target date to January motohi

Third, resolve key points

1. Analyzing leap year

To solve this program we need to define a function before the main function is used to determine whether a year is a leap year.

int YN(int x)
{
    if(x%4!=0){
        return 0;
    }
    else if(x%100==0&&x%400!=0)
        return 0;
    else if(x%4==0)
        return 1;
}

(Due OF Xueyibujing, more primitive function, the function works for judging YN (x) value is equal to 1 if x is a leap year, equal to 0 if x is not a leap year.)

2. The number of days in each month on behalf of

Is a representative of the number of days each month, we need to define an array. Which contains the number of days each month and the initial value of 0 month, i.e. an initial natural and 12 months, a total of 13 months.

int yue[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

3. The number of days the first year target date to January motohi

To calculate the number of days the first year of January motohi to date, we need to calculate three parts: the
total number of days the first year of 1.
The total number of days all years between 2 and objectives of the first year.
The total number of days to the date of January target date of May 3. The target year.
Below the first year of January to the first Mongol target date, for example, the first year to the second Mongol January target date may be so.

for(int i1=2;i1<a1;i1++){
        tian1=tian1+365;
        if(YN(i1)==1) tian1=tian1+1;
    }
    for(int i1=2;i1<=12;i1++){
        tian1=tian1+yue[i1];
    }
    tian1=tian1+yue[1]-1;
    if(YN(1)==1){
        if(1<=2){
            tian1=tian1+1;
        }
    }
        for(int i1=1;i1<a2;i1++){
            tian1=tian1+yue[i1];
        }
        tian1=tian1+a3;
        if(YN(a1)==1){
            if(a2>2) tian1=tian1+1;
        }

program

Completed the preparatory work, we can officially begin programming.
We follow the flow chart programming, while the integration of key points, and finally add some details.
Well, this question of the program can be made:

#include<iostream>
using namespace std;

int YN(int x)
{
    if(x%4!=0){
        return 0;
    }
    else if(x%100==0&&x%400!=0)
        return 0;
    else if(x%4==0)
        return 1;
}

int main()
{
    int a1,a2,a3,b1,b2,b3;
    cin>>a1>>a2>>a3;
    cin>>b1>>b2>>b3;
    int yue[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int tian=0,tian1=0,tian2=0;
    
    if(a1==b1){
        if(a2==b2) tian=b3-a3;
        else{
            for(int i=a2+1;i<b2;i++){
                tian+=yue[i];
            }
            tian+=yue[a2]-a3+b3;
            if(YN(a1)==1){
                if(a2<=2&&b2>2) tian+=1;
            }
        }
    }
    
    else{
    for(int i1=2;i1<a1;i1++){
        tian1=tian1+365;
        if(YN(i1)==1) tian1=tian1+1;
    }
    for(int i1=2;i1<=12;i1++){
        tian1=tian1+yue[i1];
    }
    tian1=tian1+yue[1]-1;
    if(YN(1)==1){
        if(1<=2){
            tian1=tian1+1;
        }
    }
        for(int i1=1;i1<a2;i1++){
            tian1=tian1+yue[i1];
        }
        tian1=tian1+a3;
        if(YN(a1)==1){
            if(a2>2) tian1=tian1+1;
        }
        
    for(int i2=2;i2<b1;i2++){
        tian2=tian2+365;
        if(YN(i2)==1) tian2=tian2+1;
    }
    for(int i2=2;i2<=12;i2++){
        tian2=tian2+yue[i2];
    }
    tian2=tian2+yue[1]-1;
    if(YN(1)==1){
        if(1<=2){
            tian2=tian2+1;
        }
    }
                for(int i2=1;i2<b2;i2++){
            tian2=tian2+yue[i2];
        }
        tian2=tian2+b3;
        if(YN(b1)==1){
            if(b2>2) tian2=tian2+1;
        }
    tian=tian2-tian1;
    }
    cout<<tian;
    return 0;
}
Published 10 original articles · won praise 11 · views 329

Guess you like

Origin blog.csdn.net/lz28noi/article/details/104236090