每月工作日只考虑周末情况,即只有周六、周日放假。每月第一个工作日如果是星期一则该月是A类月,每月最后一个工作日如果是星期五则该月是B类月。一个月可能是A类月也可能是B类月。
如2006 10 2是该月第一个工作日为星期一是A类月,如2006 9 29是该月最后一个工作日为星期五则是B类月。
输入一个时间段如:2006 6 2006 6(闭区间),求出该时间段内的A类月和B类月数量。python或c/c++。
编程求解,如内容所示题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- _GX_ 2022-02-09 07:09关注
#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
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥30 Matlab打开默认名称带有/的光谱数据
- ¥50 easyExcel模板 动态单元格合并列
- ¥15 res.rows如何取值使用
- ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
- ¥15 CSP算法实现EEG特征提取,哪一步错了?
- ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
- ¥15 vue3前端取消收藏的不会引用collectId
- ¥15 delphi7 HMAC_SHA256方式加密
- ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
- ¥15 下列c语言代码为何输出了多余的空格