题目描述 :
作为一个关心父母的孩子,Compute 会在每年的母亲节和父亲节为父母准备礼物。可是粗心的他却不记得它们的具体日期了。
已知:母亲节在每年 5 月的第 2 个周日;父亲节在每年 6 月的第 3 个周日。
现在你需要告诉他,下一个(不包括当天)母亲节或父亲节是在什么时候。
输入描述:
第一行包含一个整数 T (T \leq 100T≤100),表示测试数据的组数。
对于每组数据,包含三个整数 y, m, d,中间以空格分隔,分别表示今天的年、月、日。
输入保证是一个在公元 2000 年 1 月 1 日 到 2100 年 12 月 31 日间的合法日期。
输出描述:
对于每组数据,在一行输出下一个需要准备礼物的节日和日期。格式参考样例输出。
示例1:
输入:
7
2000 1 1
2001 1 1
2002 1 1
2003 1 1
2020 1 1
2020 5 10
2020 6 21
输出:
Mother's Day: May 14th, 2000
Mother's Day: May 13th, 2001
Mother's Day: May 12th, 2002
Mother's Day: May 11th, 2003
Mother's Day: May 10th, 2020
Father's Day: June 21st, 2020
Mother's Day: May 9th, 2021
备注:
注意在冒号和逗号后面有一个空格。
代码:
#include<iostream>
using namespace std;
bool isLeap(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int reweek(int y, int m, int d)
{
int year = 2000;
int week = 6;
int day = 1, month = 1;
while (year < y)
{
if (isLeap(year))
{
week += 2;
week %= 7;
}
else
{
week += 1;
week %= 7;
}
year++;
}
while (day != d || month != m)
{
day++;
week++;
week %= 7;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)
{
if (day == 32)
{
day = 1;
month++;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day == 31)
{
day = 1;
month++;
}
}
else if (month == 2)
{
if (isLeap(year) && day == 30)
{
day = 1;
month++;
}
else if (!isLeap(year) && day == 29)
{
day = 1;
month++;
}
}
else if (month == 12)
{
if (day == 32)
{
year++;
month = 1;
day = 1;
}
}
}
return week;
}
bool mother_judge(int month, int day,int week)
{
return month == 5 && week == 0 && 8 <= day && day <= 14;
}
bool father_judge(int month, int day, int week)
{
return month == 6 && week == 0 && 15 <= day && day <= 21;
}
int main()
{
int t;
int y, m, d;
int year, month, day;
int week;
cin >> t;
while (t--)
{
cin >> y >> m >> d;
year = y;
month = m;
day = d + 1;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)
{
if (day == 32)
{
day = 1;
month++;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day == 31)
{
day = 1;
month++;
}
}
else if (month == 2)
{
if (isLeap(year) && day == 30)
{
day = 1;
month++;
}
else if (!isLeap(year) && day == 29)
{
day = 1;
month++;
}
}
else if (month == 12)
{
if (day == 32)
{
year++;
month = 1;
day = 1;
}
}
week = reweek(year, month, day);
while (!father_judge(month, day, week) && !mother_judge(month, day, week))
{
day++;
week++;
week %= 7;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)
{
if (day == 32)
{
day = 1;
month++;
}
}
else if (month==4||month==6||month==9||month==11)
{
if (day == 31)
{
day = 1;
month++;
}
}
else if (month == 2)
{
if (isLeap(year)&&day==30)
{
day = 1;
month++;
}
else if (!isLeap(year) && day == 29)
{
day = 1;
month++;
}
}
else if (month == 12)
{
if (day == 32)
{
year++;
month = 1;
day = 1;
}
}
}
if (month == 6)
{
printf("Father's Day: June ");
if (15 <= day && day <= 20)
printf("%dth, ", day);
else if (day == 21)
printf("21st, ");
printf("%d\n", year);
}
else if (month == 5)
{
printf("Mother's Day: May %dth, %d\n", day, year);
}
}
return 0;
}