题解 第二章暴力求解| #Day of Week#
Day of Week
http://www.nowcoder.com/practice/a3417270d1c0421587a60b93cdacbca0
我debug了很长时间
!(Y % 4) && Y % 100 || !(Y % 400)
此处必须要在!
后打括号,否则会出错
思路: 本题只需求出,今天到1000-01-01日的天数,加上一个bias
随后对7的余数
即为对应的星期
因为星期是和相差的天数息息相关的
#include <bits/stdc++.h>
using namespace std;
static const vector<string> monthsName =
{"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
static const vector<string> weekdayName =
{"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
static const int month[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}};
static const int year[2] = {365, 366};
int main()
{
int D = 0, M = 0, Y = 0;
string mon;
while (cin >> D)
{
cin >> mon;
// https://blog.csdn.net/qnavy123/article/details/86977740
// https://www.cnblogs.com/cjlaaa/p/3236727.html
for (int i = 0; i < monthsName.size(); i++)
if (mon == monthsName[i])
{
M = i + 1;
break;
}
cin >> Y;
//以上即得到了年月日
int isRun = 0, PassDaysThisYear = 0, PassYearsDays = 0;
if (!(Y % 4) && Y % 100 || !(Y % 400))
isRun = 1;
else
isRun = 0;
for (int i = 0; i < M; i++)
PassDaysThisYear += month[isRun][i];
PassDaysThisYear += D;
//以上求出今年过去了多长时间
for (int i = 1000; i < Y; i++)
{
if (!(i % 4) && (i % 100) ||!((i % 400)))
isRun = 1;
else
isRun = 0;
PassYearsDays += year[isRun];
}
//以上求出今年之前到1000-01-01的时间
cout << weekdayName[(PassYearsDays + PassDaysThisYear + 1) % 7] << endl;
}
return 0;
}
王道机试指南刷题 文章被收录于专栏
计划刷完这本书