May Day Holiday——————日期处理,水题

May Day Holiday

Time Limit: 2 Seconds Memory Limit: 65536 KB
As a university advocating self-learning and work-rest balance, Marjar University has so many days of rest, including holidays and weekends. Each weekend, which consists of Saturday and Sunday, is a rest time in the Marjar University.

The May Day, also known as International Workers’ Day or International Labour Day, falls on May 1st. In Marjar University, the May Day holiday is a five-day vacation from May 1st to May 5th. Due to Saturday or Sunday may be adjacent to the May Day holiday, the continuous vacation may be as long as nine days in reality. For example, the May Day in 2015 is Friday so the continuous vacation is only 5 days (May 1st to May 5th). And the May Day in 2016 is Sunday so the continuous vacation is 6 days (April 30th to May 5th). In 2017, the May Day is Monday so the vacation is 9 days (April 29th to May 7th). How excited!

Edward, the headmaster of Marjar University, is very curious how long is the continuous vacation containing May Day in different years. Can you help him?

Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case, there is an integer y (1928 <= y <= 9999) in one line, indicating the year of Edward’s query.

Output
For each case, print the number of days of the continuous vacation in that year.

Sample Input
3
2015
2016
2017
Output
5
6
9
Author: ZHOU, Yuchen
Source: The 12th Zhejiang Provincial Collegiate Programming Contest


给你一个年份,问你这一年五一放几天假,如果放假时间段和周末时间有重叠的话,要把周末也加进去
就是处理一下时间就好了。

正常的代码在这里

#include
using namespace std;
const int MAXN = 1e5+7;
int a[MAXN];
int w[MAXN];
bool check(int year)
{
	if(year%400==0||(year%100!=0&&year%4==0))	return 1;
	return 0;
}

void init()
{
	memset(a,0,sizeof(a));
	memset(w,0,sizeof(w));
	w[1928]=2;
	a[1928]=6;
	int d = 2;
	for(int i=1929;i<=10000;++i)
	{
		if(check(i))	d+=366;
		else		d+=365;
		d = (d%7+7)%7;
		switch(d%7)
		{
			case 0: w[i] = 7; a[i] = 6; break; 
			case 1: w[i] = 1; a[i] = 9; break;
			case 2: w[i] = 2; a[i] = 6; break;
			case 3: w[i] = 3; a[i] = 5; break;
			case 4: w[i] = 4; a[i] = 5; break;
			case 5: w[i] = 5; a[i] = 5; break;
			case 6: w[i] = 6; a[i] = 5; break;
		}
	}
	
}


int main()
{
	init();
	int n,day;
	cin>>n;
	while(n--)
	{
		cin>>day;
//		cout<
		cout<<a[day]<<endl;
	 } 
	return 0;
}

因为想要复习一下蔡勒公式
蔡乐公式:
复杂度:O(1)

#include
using namespace std;
typedef long long ll;
const int MAXN = 1e6+7;
int w[MAXN];

int whatday(int y, int m, int d)//模板,记住就好
{
        int ans;
        if(m==1||m==2)
                m += 12, y--;
         if((y<1752)||(y==1752&&m<9)||(y==1752&&m==9&&d<3))
                ans = (d + 2*m + 3*(m+1)/5 + y + y/4 + 5)%7;
        else
                ans = (d + 2*m + 3*(m+1)/5 + y + y/4 -y/100 + y/400)%7;
        return ans+1;
}

int main()
{
        // freopen("../in.txt","r",stdin);
        // freopen("../out.txt","w",stdout);
        int t;
        cin>>t;
        while(t--)
        {
                int y;
                cin>>y;
                int week = whatday(y,5,1);
                int ans = 0;
                int  i = 0;
                switch(week)
		{

			case 1: w[i] = 1; ans = 9; break;
			case 2: w[i] = 2; ans = 6; break;
			case 3: w[i] = 3; ans = 5; break;
			case 4: w[i] = 4; ans = 5; break;
			case 5: w[i] = 5; ans = 5; break;
			case 6: w[i] = 6; ans = 5; break;
                        case 7: w[i] = 7; ans = 6; break;
		}
                cout<<ans<<endl;
        }
        return 0;
}

你可能感兴趣的:(模拟)