链接:https://ac.nowcoder.com/acm/contest/5278/F
来源:牛客网
题目描述
作为一个关心父母的孩子,Compute 会在每年的母亲节和父亲节为父母准备礼物。可是粗心的他却不记得它们的具体日期了。
已知:母亲节在每年 5 月的第 2 个周日;父亲节在每年 6 月的第 3 个周日。
现在你需要告诉他,下一个(不包括当天)母亲节或父亲节是在什么时候。
输入描述:
第一行包含一个整数 T (T \leq 100T≤100),表示测试数据的组数。
对于每组数据,包含三个整数 y, m, d,中间以空格分隔,分别表示今天的年、月、日。
输入保证是一个在公元 2000 年 1 月 1 日 到 2100 年 12 月 31 日间的合法日期。
输出描述:
对于每组数据,在一行输出下一个需要准备礼物的节日和日期。格式参考样例输出。
输入
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
思路:由题目可知,他要求的是输出是下一个父亲节或者母亲节,而且父亲节在母亲节的前面,所以要先判断父亲节,如果符合条件就不用判断母亲节,如果不符合那就判断母亲节,如果两个都不符合那就判断下一年的父亲节。其中有一个就是判断每年的5月1日和6月1日是星期几,这是一个难点,但是由公用的函数可以快速解决这个问题,直接看代码。
#include<bits/stdc++.h>
using namespace std;
int Date(int y,int m,int d) //这就是判断每年的5月1日和6月1日是星期几的函数,可以直接套用
{
if(m==1||m==2){
m+=12;
y--;
}
int week = (d + 2*m +3*(m+1)/5 + y + y/4 - y/100 + y/400 + 1)%7;
return week;
}
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
int year, mouth, day;
cin >> year >> mouth >> day;
int muw = Date(year, 5, 1);
if(muw == 0)
muw = 7;
int muday = 7 - muw + 8; //5月的第二个星期日的号数
if(mouth < 5 || (mouth == 5 && day < muday)) //判断符不符合今年的父亲节
{
cout << "Mother's Day: May " << muday;
cout << "th, " << year << endl;
continue;
}
int fuw = Date(year, 6, 1);
if(fuw == 0)
fuw = 7;
int fuday = 7 - fuw + 15; ////6月的第三个星期日的号数
if(mouth < 6 || (mouth == 6 && day < fuday)) //判断符不符合今年的母亲节
{
cout << "Father's Day: June " << fuday;
if(fuday == 21){
cout << "st, " << year << endl; //这几个日子后缀是不一样的
}
else if(fuday == 22){
cout << "nd, " << year << endl;
}
else if(fuday == 23){
cout << "rd, " << year << endl;
}
else if(fuday == 31){
cout << "st, " << year << endl;
}
else{
cout << "th, " << year << endl;
}
continue;
}
year++; //因为他只是让你求母亲节或父亲节,所以上面的条件都不符合的情况,那最近的就是第二年的父亲节,不用求母亲节
muw = Date(year, 5, 1);
if(muw == 0)
muw = 7;
muday = 7 - muw + 8;
cout << "Mother's Day: May " << muday;
cout << "th, " << year << endl;
}
}