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

题目描述

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

题意:给你四个值a,b,c,y,a为月份,b c为第几个周几,y年份,判断第b个周c是否合法,如果合法就按格式输出

思路:求出每一个月的第一天是周几(蔡勒公式,用map存一下每年对应的每一个月,求出题目给定的第b个周c是多少号,判断是否再当前月内(输出格式我忘记了%02d可以控制,,我直接多了十几行代码

参考代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<cmath>
using namespace std;
#define ll long long int
ll quickMod(ll a, ll b, ll c)
{
    ll ans = 1;
    while (b)
    {

        if (b&1)
            ans = (ans * a) % c;
        b >>= 1;
        a = (a * a) % c;
    }
    return ans;
}
int weekday(int Y,int M,int D)
{
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    Y -= M < 3;
    return (Y + Y/4 - Y/100 + Y/400 + t[M-1] + D) % 7;
}
bool check(int Y)
{
    if(Y%4==0&&Y%100||Y%400==0)
        return 1;
    return 0;
}
int get_length(int x)
{
    using namespace std;
    int leng=0;
    while(x)
    {
        x/=10;
        leng++;
    }
    return leng;
}
map<int,int>sum;
int main()
{
    int a,b,c,y;
    while(cin>>a>>b>>c>>y)
    {
        sum[1]=31,sum[3]=31,sum[4]=30,sum[5]=31,sum[6]=30,sum[7]=31,sum[8]=31,sum[9]=30,sum[10]=31,sum[11]=30,sum[12]=31;
        if(check(y))
            sum[2]=29;
        else
            sum[2]=28;
        int w=weekday(y,a,1);
        int day=sum[a];
        if(w==0)
            w=7;
        int x;
        if(c-w>=0)
            x=(b-1)*7+1+(c-w);
        else
            x=(b-1)*7-1+7+w-c;
        if(x>day)
            cout<<"none\n";
        else
        {
//            int len_y=get_length(y);
//            int len_m=get_length(a);
//            int len_x=get_length(x);
//            if(len_y<4)
//                for(int i=1; i<=4-len_y; i++)
//                    cout<<"0";
//            cout<<y<<"/";
//            if(len_m<2)
//                cout<<"0";
//            cout<<a<<"/";
//            if(len_x<2)
//                cout<<"0";
//            cout<<x<<"\n";
            printf("%04d/%02d/%02d\n",y,a,x);
        }
    }
    return 0;
}