普通小明 2022-02-09 02:31 采纳率: 100%
浏览 87
已结题

编程求解,如内容所示题

每月工作日只考虑周末情况,即只有周六、周日放假。每月第一个工作日如果是星期一则该月是A类月,每月最后一个工作日如果是星期五则该月是B类月。一个月可能是A类月也可能是B类月。
如2006 10 2是该月第一个工作日为星期一是A类月,如2006 9 29是该月最后一个工作日为星期五则是B类月。
输入一个时间段如:2006 6 2006 6(闭区间),求出该时间段内的A类月和B类月数量。python或c/c++。

  • 写回答

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 2月17日
  • 已采纳回答 2月9日
  • 创建了问题 2月9日

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败