Question
已知:母亲节在每年 5 月的第 2 个周日;父亲节在每年 6 月的第 3 个周日。
输入2000 年 1 月 1 日 到 2100 年 12 月 31 日间的合法日期,求最近的母亲节或父亲节在哪天?
Solution
打表
英语日期:除了11,12,13之外其他所有的以1,2,3结尾的尾缀为st nd rd。
闰年:
- 普通闰年:公历年份是4的倍数的,且不是100的倍数,为普通闰年。(如2004年就是闰年);
- 世纪闰年:公历年份是整百数的,必须是400的倍数才是世纪闰年(如1900年不是世纪闰年,2000年是世纪闰年);
估计很多人和我一样一开始没特判2100年吧。
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll maxn = 1e6 + 5;
const int N = 5e5 + 5;
struct node{
int y,m,d,sex;
}a[40000];
int f[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int tot=0;
void init(){
int t=4;
for(int i=2000;i<2100;i++){
for(int j=1;j<=12;j++){
int cnt=0;
int kkk=0;
bool ok1=false,ok2=false;
for(int k=1;k<=f[j];k++){
t=(t+1)%7;
if(j==5&&t==6){
cnt++;
}
if(cnt==2&&!ok1) {ok1=true;a[++tot]={i,j,k,0};}
if(j==6&&t==6){
kkk++;
}
if(kkk==3&&!ok2) {ok2=true;a[++tot]={i,j,k,1};}
}
if(j==2 && i%4==0){
t=(t+1)%7;
}
}
}
a[++tot]={2100,5,9,0};
a[++tot]={2100,6,20,1};
}
void print(int x){
if(a[x].sex==1) cout<<"Father's Day: June ";
else cout<<"Mother's Day: May ";
int k=a[x].d;
cout<<a[x].d;
if(k==1||k==21||k==31) cout<<"st";
else if(k==2||k==22) cout<<"nd";
else if(k==3||k==23) cout<<"rd";
else cout<<"th";
cout<<", "<<a[x].y<<endl;
}
bool check(int x,int Y,int M,int D){
int y=a[x].y;
int m=a[x].m;
int d=a[x].d;
if(y>Y){
return true;
}
else if(y==Y){
if(m>M) return true;
else if(m==M){
if(d>D) return true;
return false;
}
return false;
}
return false;
}
void solve(){
int Y,M,D;cin>>Y>>M>>D;
for(int i=1;i<=tot;i++){
if(check(i,Y,M,D)){
print(i);
break;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
init();
int T;cin>>T;
while(T--){
solve();
}
return 0;
}
京公网安备 11010502036488号