牛客科大讯飞杯 日期小助手 _暴力模拟

传送门


一个很有趣的模拟题:给你一个日期,问你最近的后面一个母亲节或父亲节是什么时候。

在这里插入图片描述


可以看到数据范围只有一百年,也就是100*365 * 100组数据,显然可以暴力。

先打出这一百年里边的母亲节和父亲节的表,然后每次输入一个日期,暴力++、判断即可。

AC代码:

//https://blog.csdn.net/hesorchen
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI cos(-1)
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000009
#define lowbit(abcd) (abcd & (-abcd))

bool check(int y)
{
    if (y % 4 == 0 || (y % 100 != 0 && y % 400 == 0))
        return 1;
    return 0;
}
string w[404] = {"", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"};
int p[5];
int ans_mother[1000][5];
int ans_father[1000][5];
int ct = 1, ct2 = 1;
int d[15] = {0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main()
{
    p[1] = 2000;
    p[2] = 1;
    p[3] = 1;
    int ff;
    int fff;
    while (p[1] <= 2100)
    {
        if (check(p[1]))
            d[2] = 29;
        else
            d[2] = 28;
        p[3]++;
        if (p[3] > d[p[2]])
        {
            p[3] -= d[p[2]];
            p[2]++;
            if (p[2] > 12)
            {
                p[2] -= 12;
                p[1]++;
            }
        }
        int flag = 0;
        int flagg = 0;

        if (p[2] == 5)
        {
            fff = 0;
            if ((p[3] + 2 * p[2] + 3 * (p[2] + 1) / 5 + p[1] + p[1] / 4 - p[1] / 100 + p[1] / 400 + 1) % 7 == 0)
            {
                flag = 1;
                ff++;
            }
            if (ff == 2 && flag)
            {
                ff = 3;
                ans_mother[ct][1] = p[1];
                ans_mother[ct][2] = p[2];
                ans_mother[ct++][3] = p[3];
            }
        }
        else if (p[2] == 6)
        {
            ff = 0;
            if ((p[3] + 2 * p[2] + 3 * (p[2] + 1) / 5 + p[1] + p[1] / 4 - p[1] / 100 + p[1] / 400 + 1) % 7 == 0)
            {
                flagg = 1;
                fff++;
            }
            if (fff == 3 && flagg)
            {
                fff = 4;
                ans_father[ct2][1] = p[1];
                ans_father[ct2][2] = p[2];
                ans_father[ct2++][3] = p[3];
            }
        }
        else
            ff = 0;
    }
    int t;
    cin >> t;
    while (t--)
    {
        cin >> p[1] >> p[2] >> p[3];
        while (1)
        {
            if (check(p[1]))
                d[2] = 29;
            else
                d[2] = 28;
            p[3]++;
            if (p[3] > d[p[2]])
            {
                p[3] -= d[p[2]];
                p[2]++;
                if (p[2] > 12)
                {
                    p[2] -= 12;
                    p[1]++;
                }
            }
            int ffff = 0;
            for (int i = 1; i < ct; i++)
                if (ans_mother[i][1] == p[1] && ans_mother[i][2] == p[2] && ans_mother[i][3] == p[3])
                {
                    printf("Mother's Day: May %d%s, %d\n", p[3], w[p[3]].c_str(), p[1]);
                    ffff = 1;
                    break;
                }
            if (!ffff)
                for (int i = 1; i < ct2; i++)
                    if (ans_father[i][1] == p[1] && ans_father[i][2] == p[2] && ans_father[i][3] == p[3])
                    {
                        printf("Father's Day: June %d%s, %d\n", p[3], w[p[3]].c_str(), p[1]);
                        ffff = 1;
                        break;
                    }
            if (ffff)
                break;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值