// 经典模板
// months[]
// int get_days()
// bool is_leap()
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
int months[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31
};
unordered_map<string,int> month_name={
{"January",1},
{"February",2},
{"March",3},
{"April",4},
{"May",5},
{"June",6},
{"July",7},
{"August",8},
{"September",9},
{"October",10},
{"November",11},
{"December",12},
};
bool is_leap(int year)
{
return year%400==0 || year%4==0 && year%100;
}
int get_days(int year,int month)
{
int s=months[month];
if(month==2) s+=is_leap(year);
return s;
}
int main()
{
int d,y;
string month;
while(cin>>d>>month>>y)
{
int m=month_name[month];
int i=1,j=1,k=1; //日 月 年 从1 1 1 开始
int days=0; //第一天是星期一,下标为0
while(i<d || j<m || k<y)
{
days++;
i++;
if(i>get_days(k,j))
{
i=1;
j++;
if(j>12)
{
j=1; //回到第一月
k++;
}
}
}
int today=days%7;
if(today==0)
{
cout<<"Monday"<<endl;
}
else if(today==1) cout<<"Tuesday"<<endl;
else if(today==2) cout<<"Wednesday"<<endl;
else if(today==3) cout<<"Thursday"<<endl;
else if(today==4) cout<<"Friday"<<endl;
else if(today==5) cout<<"Saturday"<<endl;
else cout<<"Sunday"<<endl;
}
}
直接用if else判断比用哈希表快一些
// 经典模板
// months[]
// int get_days()
// bool is_leap()
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
int months[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31
};
unordered_map<string,int> month_name={
{"January",1},
{"February",2},
{"March",3},
{"April",4},
{"May",5},
{"June",6},
{"July",7},
{"August",8},
{"September",9},
{"October",10},
{"November",11},
{"December",12},
};
unordered_map<int,string> Day_Name={
{0,"Monday"},
{1,"Tuesday"},
{2,"Wednesday"},
{3,"Thursday"},
{4,"Friday"},
{5,"Saturday"},
{6,"Sunday"},
};
bool is_leap(int year)
{
return year%400==0 || year%4==0 && year%100;
}
int get_days(int year,int month)
{
int s=months[month];
if(month==2) s+=is_leap(year);
return s;
}
int main()
{
int d,y;
string month;
while(cin>>d>>month>>y)
{
int m=month_name[month];
int i=1,j=1,k=1; //日 月 年 从1 1 1 开始
int days=0; //第一天是星期一,下标为0
while(i<d || j<m || k<y)
{
days++;
i++;
if(i>get_days(k,j))
{
i=1;
j++;
if(j>12)
{
j=1; //回到第一月
k++;
}
}
}
int today=days%7;
cout<<Day_Name[today]<<endl;
}
}
```