[Date] holiday type title

topic

There are calendar  solar calendar (Gregorian calendar)  and the  lunar calendar (lunar calendar)  of the points. Every year holidays, which fall into three categories - day weekend, holiday Gregorian, lunar holidays.

Day weekend

  • Saturday and Sunday 22 days

Gregorian holidays

  1. New Year's Day: Gregorian calendar year January 1, 11 days holiday
  2. Labor Day: May 1 Gregorian calendar year, 11 days holiday
  3. National Day: October 1 Gregorian calendar year, 33 days holiday
  4. Christmas: Gregorian calendar each year on December 25, 11 days holiday

Lunar Holidays

  • Chinese New Year: January 1st lunar calendar every year, 33 days holiday
  • Ching Ming Festival: April 4 solar calendar every year - between the 6th day, 11 days holiday
  • Dragon Boat Festival: May 5th lunar calendar every year, 11 days holiday
  • Mid-Autumn Festival: lunar calendar every year on August 15, 11 days holiday

When the holidays and weekend coincides weekend not delayed nor in advance to ensure that no overlap between the holidays. Now to give you a year of all lunar holidays Gregorian date, and year of January 1 of the week, you calculate this year (Gregorian calendar on January 1st to December 31st) put the number of days off (including weekend, holidays and lunar solar calendar holidays).

Input Format

The first line of input year y (1900 <y≤2050). The next 44 lines of the input two integers m, d, sequentially showing Gregorian date Spring, festival, Dragon Boat Festival and the Mid. The last line represents an integer year is January 1 (the first few days of the week, every week from Monday to start counting, that Monday is the first day) of the week.

Output Format

Output An integer representing the number of days of holiday that year.

Sample input

2017
1 28
4 4
5 30
10 4
7

Sample Output

113

Analytical title

About the date , we can build a day array, used to record the number of days each month, you can help us to traverse. (Do not worry first days of February, a different situation, we can determine whether the main modification is a leap year to go)

Eighty one thousand three hundred fifty-seven Prince, never poor thirty-one days.

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

Next, we deal with the issue in February of a leap year.

Current year divisible by 4 or a hundred years divisible by 400 are leap years.

According to this rule, you can modify the number of days in February.

if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0){
        day[2]++;
}

Next, we use mm array and dd array to store the date of the holiday.

Gregorian to enter holidays, because it is fixed, it can be directly written into the array.

int mm[10] = {1, 5, 10, 10, 10, 12};
int dd[10] = {1, 1, 1, 2, 3, 25};

The lunar holidays are likely to change every year, so the user input is required, we will enter the array can be written.

But here it should be noted that, because the Spring Festival three days, so we carry out a separate process. Use variable f record Spring days remaining, when f = 0 when recording is completed Spring described.

After the Spring Festival judgment, we need to calculate the weekend, weekend calculated directly using the variable w to record the day of the week. When w = 8, when set to 1.

After exclusion of the weekend, the last to judge the rest of the holiday, which is good to do, and can be matched directly.

Note: The counting process is a cycle, each cycle needs to end the next day, this time we need a nextday function to help us to date to be modified.

Counting whole cycles were as follows:

    while(m != 13){
        //先判春节
        if(m == mm[6] && d == dd[6]){
            ans++;//是假期
            f = 2;
        }else if(f){
            ans++;
            f--;//春节少一天了
        }else if(w == 6 || w == 7){
            //周末
            ans++;
        }else{
            for(int i = 0; i < 10; i++){
                if(m == mm[i] && d == dd[i]){
                    ans++;
                    break;
                }
            }
        }
        nextday(m, d);
        w = w + 1;
        if(w == 8) 
            w = 1;
    }

Nextday function parameters are two references to amend the month and day.

int nextday(int &m, int &d){
    if(d == day[m]){
        m++;
        d = 1;
    }else{
        d++;
    }
}

The complete code

#include<bits/stdc++.h>
using namespace std;
int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int mm[10] = {1, 5, 10, 10, 10, 12};
int dd[10] = {1, 1, 1, 2, 3, 25};
int nextday(int &m, int &d){
    if(d == day[m]){
        m++;
        d = 1;
    }else{
        d++;
    }
}
int main(){
    int y, w, m, d, f, ans;
    scanf("%d", &y);
    if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0){
        day[2]++;
    }
    for(int i = 6; i < 10; i++){
        scanf("%d %d", &mm[i], &dd[i]);
    }
    scanf("%d", &w);
    m = 1;
    d = 1;
    ans = 0;
    f = 0;
    while(m != 13){
        //先判春节
        if(m == mm[6] && d == dd[6]){
            ans++;//是假期
            f = 2;
        }else if(f){
            ans++;
            f--;//春节少一天了
        }else if(w == 6 || w == 7){
            //周末
            ans++;
        }else{
            for(int i = 0; i < 10; i++){
                if(m == mm[i] && d == dd[i]){
                    ans++;
                    break;
                }
            }
        }
        nextday(m, d);
        w = w + 1;
        if(w == 8) 
            w = 1;
    }
    printf("%d", ans);
    return 0;
}

 

Published 62 original articles · won praise 34 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_41960890/article/details/105266826