AcWing 3489. 星期几 傻瓜解法
原题链接
中等
作者:
sixdog
,
2021-05-26 14:09:09
,
所有人可见
,
阅读 173
(暴力枚举) 无优化
C++ 代码
#include <iostream>
#include <string>
using namespace std;
int main() {
string a[12]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
string x;
int m=0,y,d,sum=0;
while(cin>>d>>x>>y) {
sum=0;
for(int i=0; i<12; i++) {
if(a[i]==x) {
m=i+1;
break;
}
}
for(int i=1; i<=y-1; i++) {
if((i%400==0) || ((i%4==0) && (i%100!=0))) {
sum=sum+366;
} else {
sum=sum+365;
}
}
for(int i=1; i<=m-1; i++) {
if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10) {
sum=sum+31;
} else if(i==2) {
if((y%4==0 && y%100!=0) || (y%400==0)) {
sum=sum+29;
} else {
sum=sum+28;
}
} else {
sum=sum+30;
}
}
sum=sum+d;
switch(sum%7) {
case 1:
cout<<"Monday\n";
break;
case 2:
cout<<"Tuesday\n";
break;
case 3:
cout<<"Wednesday\n";
break;
case 4:
cout<<"Thursday\n";
break;
case 5:
cout<<"Friday\n";
break;
case 6:
cout<<"Saturday\n";
break;
case 0:
cout<<"Sunday\n";
break;
}
}
return 0;
}