题目描述
作为一个关心父母的孩子,Compute 会在每年的母亲节和父亲节为父母准备礼物。可是粗心的他却不记得它们的具体日期了。
已知:母亲节在每年 5 月的第 2 个周日;父亲节在每年 6 月的第 3 个周日。
现在你需要告诉他,下一个(不包括当天)母亲节或父亲节是在什么时候。
输入描述:
第一行包含一个整数 T (T≤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
题解:模拟+吉姆拉尔森计算星期公式
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<sstream>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3f;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
string str[50]={"",
"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"};
ll s_day(ll y,ll m,ll d)
{
ll day=d;
ll flag=0;
ll d1[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,
0,31,29,31,30,31,30,31,31,30,31,30,31};
if(y%4==0&&y%100!=0||y%400==0)
flag=1;
for(int i=0;i<m;i++)
{
day+=d1[flag][i];
}
return day;
}
ll get_week(ll y,ll m,ll d)
{
if(m<3)
{
m+=12;
y--;
}
if((d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7==0)
return 7;
else
return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
}
int main()
{
io();
ll t,n;
ll wm,wf,md,mf;
cin>>t;
while(t--)
{
ll y,m,d;
cin>>y>>m>>d;
ll day=s_day(y,m,d);//输入当年天数
//cout<<get_week(y,m,d)<<endl;
wm=s_day(y,4,30)+14-get_week(y,5,1)+1;//当年母亲节的实际天数
wf=s_day(y,5,31)+21-get_week(y,6,1)+1;//当年父亲节的实际天数
if(day<wm)
cout<<"Mother's Day: May "<<str[14-get_week(y,5,1)+1]<<", "<<y<<endl;
else if(day>=wm&&day<wf)
cout<<"Father's Day: June "<<str[21-get_week(y,6,1)+1]<<", "<<y<<endl;
else if(day>=wf)
cout<<"Mother's Day: May "<<str[14-get_week(y+1,5,1)+1]<<", "<<y+1<<endl;
}
return 0;
}