Mother's Day 哈尔滨工程大学ACM预热赛 (基姆拉尔森计算公式)

链接:https://ac.nowcoder.com/acm/contest/554/E
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

Mother's Day is a celebration honoring the mother of the family, as well as motherhood, maternal bonds, and the influence of mothers in society, which is celebrated on various days in many parts of the world, but mostly, the date is on the second Sunday in May. 
    Like Mother's Day, there are also several celebrations have no fixed date, but are set in the form of "the Bth(st/nd) weekday C of the Ath(st/nd) month in a year", for example, as above, Mother's Day is on the second Sunday in May of a year, but also can be described as "the 2nd weekday 7 of the 5st month in a year"(A=5, B=2, C=7). 
    Now, to help remember these special days, HVT wants you to calculate all the exact date of such celebrations in the past and future hundreds of years, which means that you are given 4 numbers: A(1 ≤ A ≤ 12), B(B ≥ 1), C(1 ≤ C ≤ 7, 1=Monday,2=Tuesday,...,7=Sunday) and y(represents the year, 1850 ≤ y ≤ 2050), and you should write code to calculate the exact date of "the Bth(st/nd) weekday C of the Ath(st/nd) month in a year y".
    For your convenience, we will noice you that: January 1st, 1850 is a Tuesday.

输入描述:

The input contains multiple lines of test data, each line has 4 numbers: A B C and y.

输出描述:

For each input, you should output a exact date in the format of "yyyy/mm/dd"(When the number of digits is insufficient, 0 should be added to the front);
if the Bth(st/nd) weekday C of the Ath(st/nd) month in a year y does not exist(for example, there will never be a 7th Monday in any months of any year), you should output "none" (without quotes).

示例1

输入

复制

4 2 7 2018
4 1 7 2018
2 5 4 2018
2 4 3 2018

输出

复制

2018/04/08
2018/04/01
none
2018/02/28

直接套公式

暴力枚举即可

#include<cstdio>
#include<iostream>
using namespace std;
int cal(int y,int m, int d)
{
    if(m==1||m==2) {
        m+=12;
        y--;
    }
    int iWeek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
  	return iWeek;
}
int run(int x)
{
    if(x%4==0&&(x%100!=0||x%400==0)) return 1;
    return 0;
}
int month[20]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    int A,B,C,Y;
    while(~scanf("%d%d%d%d",&A,&B,&C,&Y))
    {
        int tmp;
        tmp= (run(Y)==1&&A==2)?month[A]+1:month[A];
        int cnt=0;
        int flag=0;
        //cout<<tmp<<"}"<<endl;
        for(int i=1;i<=tmp;i++)
        {
            //cout<<cal(Y,A,i)<<"&"<<endl;
            if(cal(Y,A,i)+1==C) cnt++;
            if(cnt==B)
            {
                flag=1;
                printf("%04d/%02d/%02d
",Y,A,i);
                break;
            }
        }
        if(flag==0) puts("none");
    }
}
原文地址:https://www.cnblogs.com/caowenbo/p/11852288.html