F.日期小助手

F.日期小助手

题目描述

作为一个关心父母的孩子,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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

aaHua_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值