HDU 6112 Today's He Xi "2017 Baidu Star 1005"

This problem is mainly to solve the direct date difference between two dates. If the difference %7 between the two dates is 0, it means it is the same Sunday. The most troublesome thing to solve the date difference is to solve the problem of February 29.

The February 29 issue has the following points to consider:

1. Whether the first year after that will pass through February 29, if it passes or the current year is not a leap year, it will be +365 to the next year today (if the next year is not a leap year)

2. In each subsequent year, if it is a leap year, add 366, otherwise 365

3. When judging (that is, judging whether it is the last year), it is necessary to judge whether the year is a leap year. If it is, then whether the current date exceeds February 29, and it exceeds the judgment of adding an extra day, and it does not exceed the direct addition of 365 days.

The above details are annotated in the relevant parts of the code, which can be viewed in combination with the code, and the code is attached

#include <iostream>
#include <stdio.h>

using namespace std;

bool rn(int n){ //determine whether it is a leap year
    if(n%4==0&&n%100!=0||n%400==0)
        return true;
    else
        return false;
}
intmain()
{
    int t;
    cin>>t;
    int y,m,d;
    while(t--){
        int temp=0;
        scanf("%d-%d-%d",&y,&m,&d);
        if(m==2&&d==29){ //Special case: the input is February 29
            for(int i=y+1;;i++){
                if(rn(i)) {
                    temp+=366;
                }else
                    temp+=365;
                if(temp%7==0&&rn(i)){
                    cout<<i<<endl;
                    break;
                }
            }
        }else{
            for(int i=y;;i++){
                if(rn(i)&&i==y&&m>2) { //Judgment whether the first year needs to experience the day of February 29
                    temp=-1;;
                }
                if(rn(i)) { //add one more day after leap year
                    temp+=366;
                }else
                    temp+=365;
                //cout<<i<<" "<<temp<<" "<<m<<endl;
                if(temp%7==0&&(rn(i+1)==0||m<=2)){ //There is no more than February or a leap year, so the last year does not need +1
                    cout<<i+1<<endl;
                    break;
                }
                if((temp+1)%7==0&&rn(i+1)&&m>2){ //This means that the last year is a leap year and has experienced the day of February 29, the number of days is naturally increased by one
                    cout<<i+1<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894548&siteId=291194637