题目链接
这道题,思路就是找到6.1,和5.1是周几,然后再把日期加上去,想法是直接暴算,但是因为我又菜又懒,就放弃了这个题,后来补题的时候,看到大佬的代码,有公式可以计算某一天日期是周几,并且,可以先进行预处理这样更方便了,偷学了一手,确实,有奇效。
这里留一下大佬的代码,以便日后参考和学习
#include<iostream>
#include<cmath>
#include<cstdio>
#include<string>
#include<algorithm>
#define ll long long
using namespace std;
string day[32]={ "0","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" };
int t,y,m,d;
struct ak
{
int md;
int fd;
}a[2110];
void init()
{
for(int y=2000;y<=2100;y++)//分别求出2000 - 2100父亲节母亲节的日期
{
int mmd=(1 + 2 * 5 + 3 * ( 5 + 1 ) / 5 + y + y / 4 - y / 100 + y / 400) % 7;//基姆拉尔森计算公式
int ffd=(1 + 2 * 6 + 3 * ( 6 + 1 ) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
a[y].md=7-mmd+1*7;//通过星期转换成日期
a[y].fd=7-ffd+2*7;
}
}
int main()
{
init();//预处理
cin>>t;
while(t--)
{
cin>>y>>m>>d;
if(m>6||m==6&&d>=a[y].fd)
{
cout<<"Mother's Day: May "<<day[a[y+1].md]<<", "<<y+1<<endl;//如果日期大于父亲节
continue;
}
if(m>5||m==5&&d>=a[y].md)//日期大于母亲节小于父亲节
{
cout<<"Father's Day: June "<<day[a[y].fd]<<", "<<y<<endl;
continue;
}
cout<<"Mother's Day: May "<<day[a[y].md]<<", "<<y<<endl;//日期小于母亲节
}
return 0;
}