ZOJ 3876 May Day Holiday(找規律||打表-基姆拉爾森計算公式(求某年某月某日星期幾))

kewlgrl發表於2016-04-03
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 integery (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


題目意思是五一節是5.1-5.5,但是如果有周六週日就連著在一起放了,問這樣每年能放多少天假。

當時就調出了window下的日曆看了一下規律:

今年是N年(平年),五一是周M,則N+1年時,五一是周M+1;今年是N年(閏年),五一是周M,則N+1年時,五一是周M+2

看了一下網上的程式碼,可以打表,方便很多,但是當時我自己就只想到了找規律這種方法。


打表:

/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:mayday.cpp
* 作    者:單昕昕
* 完成日期:2016年4月5日
* 版 本 號:v1.0
*/
#include <iostream>
using namespace std;

int main()
{
    int t,y,m=5,d=1;
    cin>>t;
    int a[7]= {6,9,6,5,5,5,5};//五一是週日到週六中的某天時放假的天數
    while(t--)
    {
        cin>>y;
        if(m==1||m==2)//此處表示把1,2月計算到上一年的13,14月
            m+=12,y--;
        int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;//基姆拉爾森計算公式(求某年某月某日星期幾) 
        cout<<a[week]<<endl;
    }
    return 0;
}

找規律:

/* 
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院 
* All rights reserved. 
* 檔名稱:zoj.cpp 
* 作    者:單昕昕 
* 完成日期:2016年4月2日 
* 版 本 號:v1.0 
*/  
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
int a[1000005];
using namespace std;
void f(int d)
{
    //cout<<"*"<<d<<endl;
    if(d==1)
        cout<<"9"<<endl;
    else if(d==2||d==7)
        cout<<"6"<<endl;
    else cout<<"5"<<endl;
}
int main()
{
    int t,i;
    scanf("%d",&t);
    int y;
    while(t--)
    {
        int d=1;
        scanf("%d",&y);
        if(y>=2000)//我這裡以2000年為基準,這天週一
        {
            for(i=2001; i<=y; ++i)
            {
                if((i%4==0&&i%100!=0)||i%400==0)//閏年
                    d+=2;
                else
                    ++d;
                if(d>7)
                    d%=7;
            }
        }
        else
        {
            for(i=2000; i>y; --i)
            {
                if((i%4==0&&i%100!=0)||i%400==0)
                    d-=2;
                else
                    --d;
                if(d==0)
                    d=7;
                if(d==-1)
                    d=6;
            }
        }
        f(d);//根據周d判斷五一放幾天
    }
    return 0;
}

相關文章