C++ calculate holidays

Exercise: Holidays
Calendars are divided into Gregorian (Gregorian) and Gregorian (lunar) calendars. There are statutory holidays every year, and these are divided into three categories—weekends, Gregorian holidays, and lunar holidays.
1. Weekends
1) Saturday and Sunday 2 days
2. Gregorian holidays
1) New Year's Day: January 1st in the Gregorian calendar, 1 day off
2) Labor Day: May 1st in the Gregorian calendar, 1 day off
3) National Day : Gregorian calendar every year on October 1st, 3 days off
4) Christmas: Gregorian calendar on December 25th every year, 1 day off
3. Lunar calendar holidays
1) Spring Festival: Lunar calendar on January 1st every year, 3 days off
2) Tomb-sweeping Day: Gregorian calendar One day between April 4-6 every year, 1 day off
3) Dragon Boat Festival: May 5th in the lunar calendar, 1 day off
4) Mid-Autumn Festival: August 15th in the lunar calendar, 1 day off
as a holiday and double When the holidays overlap, the weekends will not be delayed or advanced to ensure that there will be no overlap between holidays. Now give you the solar calendar dates of all lunar holidays in a certain year, and the day of the week on January 1 of that year, please calculate how many days of holiday (including weekends, Gregorian holidays and lunar holidays).
Input format
Input the year y (1900<y≤2050) in the first line.
In the next 4 lines, enter two integers m and d in each line to represent the Gregorian calendar dates of Spring Festival, Qingming Festival, Dragon Boat Festival and Mid-Autumn Festival in turn.
An integer in the last line indicates the day of the week on January 1 of the current year (the number of days in a week, counting from Monday every week, that is, Monday is the first day).
Output format
Output an integer, indicating the number of holiday days in the current year.
Sample input
2017
1 28
4 4
5 30
10 4
7
Sample output
113

//节假日
#include<iostream>
using namespace std;

int mm[10] = {
    
     1, 5, 10, 10, 10, 12};
int dd[10] = {
    
     1, 1, 1, 2, 3, 25 };
int day[13] = {
    
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

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

int main()
{
    
    
	int y, m, d, w, sf, ans;
	scanf("%d", &y);

	//判断闰年
	if( (y % 100 != 0 && y % 4 == 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;
	sf = 0;
	ans = 0;

	while(m < 13)
	{
    
    
		//判断春节
		if( m == mm[6] && d == dd[6])
		{
    
    
			ans++;
			sf = 2;
		}
		else if(sf)
		{
    
    
			ans++;
			sf--;
		}
		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 += 1;
		if( w == 8 )
		{
    
    
			w = 1;
		}
	}
	printf("%d", ans);

	return 0;
}

Guess you like

Origin blog.csdn.net/m0_46161051/article/details/114449185