牛客 F日期小能手

题目链接
这道题,思路就是找到6.1,和5.1是周几,然后再把日期加上去,想法是直接暴算,但是因为我又菜又懒,就放弃了这个题,后来补题的时候,看到大佬的代码,有公式可以计算某一天日期是周几,并且,可以先进行预处理这样更方便了,偷学了一手,确实,有奇效。
这里留一下大佬的代码,以便日后参考和学习

#include<iostream>
#include<cmath>
#include<cstdio>
#include<string>
#include<algorithm>
#define ll long long

using namespace std;

string day[32]={ "0","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th",
        "14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th",
        "27th","28th","29th","30th","31st" };

int t,y,m,d;

struct ak
{
    int md;
    int fd;
}a[2110];

void init()
{
    for(int y=2000;y<=2100;y++)//分别求出2000 - 2100父亲节母亲节的日期
    {
        int mmd=(1 + 2 * 5 + 3 * ( 5 + 1 ) / 5 + y + y / 4 - y / 100 + y / 400) % 7;//基姆拉尔森计算公式
        int ffd=(1 + 2 * 6 + 3 * ( 6 + 1 ) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
        a[y].md=7-mmd+1*7;//通过星期转换成日期
        a[y].fd=7-ffd+2*7;
    }
}

int main()
{
    init();//预处理
    cin>>t;
    while(t--)
    {
        cin>>y>>m>>d;
        if(m>6||m==6&&d>=a[y].fd)
        {
            cout<<"Mother's Day: May "<<day[a[y+1].md]<<", "<<y+1<<endl;//如果日期大于父亲节
            continue;
        }
        if(m>5||m==5&&d>=a[y].md)//日期大于母亲节小于父亲节
        {
            cout<<"Father's Day: June "<<day[a[y].fd]<<", "<<y<<endl;
            continue;
        }
        cout<<"Mother's Day: May "<<day[a[y].md]<<", "<<y<<endl;//日期小于母亲节
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值