每月工作日只考虑周末情况,即只有周六、周日放假。每月第一个工作日如果是星期一则该月是A类月,每月最后一个工作日如果是星期五则该月是B类月。一个月可能是A类月也可能是B类月。
如2006 10 2是该月第一个工作日为星期一是A类月,如2006 9 29是该月最后一个工作日为星期五则是B类月。
输入一个时间段如:2006 6 2006 6(闭区间),求出该时间段内的A类月和B类月数量。python或c/c++。
#include <iostream>
#include <stdexcept>
#include <ctime>
class YearMonth
{
public:
YearMonth() {}
YearMonth(int year, int month) : _year(year), _month(month)
{
if (year < 0 || month < 1 || month > 12)
throw std::invalid_argument("invalid year or month");
}
int year() const { return _year; }
int month() const { return _month; }
bool isLeapYear() const
{
return ((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0);
}
bool isTypeA() const;
bool isTypeB() const;
YearMonth &operator++()
{
if (++_month > 12)
{
_month -= 12;
++_year;
}
return *this;
}
bool operator<=(const YearMonth &other) const
{
return _year < other._year || (_year == other._year && _month <= other._month);
}
private:
int _year = 0;
int _month = 1;
};
bool YearMonth::isTypeA() const
{
std::tm tm = {.tm_mday = 1, .tm_mon = _month - 1, .tm_year = _year - 1900};
std::time_t time = std::mktime(&tm);
const std::tm *t = std::localtime(&time);
return t->tm_wday == 0 || t->tm_wday == 1 || t->tm_wday == 6;
}
bool YearMonth::isTypeB() const
{
static int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int lastDay = days[_month - 1];
if (isLeapYear() && _month == 2)
lastDay++;
std::tm tm = {.tm_mday = lastDay, .tm_mon = _month - 1, .tm_year = _year - 1900};
std::time_t time = std::mktime(&tm);
const std::tm *t = std::localtime(&time);
return t->tm_wday == 0 || t->tm_wday == 5 || t->tm_wday == 6;
}
std::istream &operator>>(std::istream &is, YearMonth &yearMonth)
{
int year, month;
is >> year >> month;
yearMonth = YearMonth(year, month);
return is;
}
int main()
{
YearMonth start, end;
std::cin >> start >> end;
int typeA = 0, typeB = 0;
while (start <= end)
{
if (start.isTypeA())
typeA++;
if (start.isTypeB())
typeB++;
++start;
}
std::cout << "Type A: " << typeA << '\n'
<< "Type B: " << typeB << '\n';
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
2006 6 2006 6
Type A: 0
Type B: 1
$ ./a.out
2006 1 2006 6
Type A: 3
Type B: 3