问题 B: Day of Week
时间限制: 1 Sec 内存限制: 32 MB
献花: 164 解决: 64
[献花][花圈][TK题库]
题目描述
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
输入
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
输出
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.
样例输入
21 December 2012
5 January 2013
样例输出
Friday
Saturday
#include
#include
#include
using namespace std;
bool isLeap(int y)
{
return (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0));
}
int MstringToI(string m)
{
string M[] = { "", "January","February","March","April","May","June","July",\
"August","September","October","November","December" };
int ind = 1;
for (; ind < 13; ++ind)
if (m == M[ind])break;
return ind;
}
int main()
{
#ifdef _DEBUG
ifstream cin("data.txt");
#endif // _DEBUG
// 0 1 2 3 4 5 6 7 8 9 10 11 12
int a[13][2] = { { 0,0 },{ 31,31 },{ 28,29 },{ 31,31 },{ 30,30 },{ 31,31 },{ 30,30 },{ 31,31 },{ 31,31 },{ 30,30 },{ 31,31 },{ 30,30 },{ 31,31 } }; //一三五七八十腊,31天永不差,四六九东30天,唯有二月28,润二29
string dofw[] = { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" };
string Adofw[] = { "","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" ,"Monday"};
int time1 = 20180101, time2, y1 = 0, y2 = 0, m1 = 0, m2 = 0, d1 = 0, d2 = 0, day = 0, i, tmp; //time1默认为20180108 星期一
int d, y; string m;
bool flag = false;//标记输入日期与默认日期的大小,默认默认时间比输入时间小
while ((cin >> d >> m >> y))
{
time2 = d + MstringToI(m) * 100 + y * 10000;
day = 0; time1 = 20180101; flag = false;
if (time1 > time2)
{
tmp = time1;
time1 = time2;
time2 = tmp;
flag = true;
}
y1 = time1 / 10000; m1 = (time1 % 10000) / 100; d1 = time1 % 100;
y2 = time2 / 10000; m2 = (time2 % 10000) / 100; d2 = time2 % 100;
if (m1 != m2) //如果m1!=m2 计算y1对应m1那个月的天数和 y2对应m2那个月的天数
{
i = isLeap(y1) ? 1 : 0;
day += a[m1][i] - d1;
day += d2;
}
if (y1 < y2) //计算y1和y2两年不同年份的天数
{
i = isLeap(y1) ? 1 : 0;
if (m1 == m2)
day += a[m1][i] - d1 + d2;
for (int k = m1 + 1; k < 13; ++k)
day += a[k][i];
i = isLeap(y2) ? 1 : 0;
for (int k = 1; k < m2; ++k)
day += a[k][i];
for (int k = y1 + 1; k < y2; ++k)
day += isLeap(k) ? 366 : 365;
}
else //同一年
{
i = isLeap(y1) ? 1 : 0;
for (int k = m1 + 1; k < m2; ++k)
day += a[k][i];
if (m1 == m2)
day += d2 - d1;
}
if (day > 6)day %= 7;
if (flag)
cout << Adofw[7 - day] << endl;
else
cout << dofw[day] << endl;
}
#ifdef _DEBUG
cin.close();
system("pause");
#endif // _DEBUG
return 0;
}