一个很有趣的模拟题:给你一个日期,问你最近的后面一个母亲节或父亲节是什么时候。
可以看到数据范围只有一百年,也就是100*365 * 100组数据,显然可以暴力。
先打出这一百年里边的母亲节和父亲节的表,然后每次输入一个日期,暴力++、判断即可。
AC代码:
#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;
}