【CCF-CSP认证真题 201712-3】【acwing.3254】 Crontab 大模拟

Crontab 大模拟

题目来源:

  1. CCF-CSP认证真题 201712-3
  2. acwing.3254 第十二次CCF计算机软件能力认证
    传送门

题目大意

类Unix系统中的任务调度器
按照特定的格式输入时间和命令
按照时间顺序输出起止时间之内的时间和命令,同时发生的指令按照输入的顺序输出
时间限制:10.0s

解题思路

时间格式分为两种,一种是输入的格式:
<minutes> <hours> <day of month> <month> <day of week> <command>
由于每个格式都可能是*,-,所以需要进行判断

  1. 如果是*,则每个合法的数值都是可以满足条件的
  2. 否则将输入的内容以,分开,再判断是范围还是具体的数,对应不同的处理

对每个位置用一个vector来存储可能出现的值,然后进行组合,并放入答案的容器中,对所有答案排序即可得到结果
由于年是不确定的,所以这里采用的方式是枚举年份,然后判断这一年的这一天的星期是否合法
细节很多,代码比较长也比较啰嗦,很多细节都放在注释里

AC代码(100分)

#include <bits/stdc++.h>
typedef long long llong;
typedef unsigned long long ullong;
const int N = 1e5 + 5;
using namespace std;
// 时间对应的结构体
struct Time {
   
    int year, month, day, hour, minute;
    Time() {
    }
    Time(int year, int month, int day, int hour, int minute) {
   
        this->year = year;
        this->month = month;
        this->day = day;
        this->hour = hour;
        this->minute = minute;
    }
	// 这里用llong来储存时间,也可以用string按位取
    Time(llong time) {
   
        minute = time % 100;    time /= 100;
        hour = time % 100;      time /= 100;
        day = time % 100;       time /= 100;
        month = time % 100;     time /= 100;
        year = time;
    }
	// 格式化,方便输出
    llong LLformat() {
   
        llong res = year;
        res = res * 100 + month;
        res = res * 100 + day;
        res = res * 100 + hour;
        res = res * 100 + minute;
        return res;
    }
    // 重载 比较运算符
    bool operator< (Time ano) {
   
        if (year == ano.year) {
   
            if (month == ano.month) {
   
                if (day == ano.day) {
   
                    if (hour == ano.hour) {
   
                        return minute < ano.minute;
                    }
                    return hour < ano.hour;
                }
                return day < ano.day;
            }
            return month < ano.month;
        }
        return year < ano.year;
    }

    bool operator>= (Time ano) {
   
        return !((*this) < ano);
    }

    bool operator== (Time ano) {
   
        return year == ano.year
            && month == ano.month
            && day == ano.day
            && hour == ano.hour
            && minute == ano.minute;
    }

};
// Crontab的结构体类型,用于存储答案
struct Crontab {
   
    Time time;
    string cmd;
    int p;	// 输入的次序,在时间相同时按照输入顺序排序
    Crontab() {
    }
    Crontab(llong time, string cmd, int p) : time(time), cmd(cmd), p(p) {
    }
    // 重载比较运算符,虽然重载了<但是仍然发生了CE,具体原因懒得找了,于是再下面又写了比较函数
    bool operator< (Crontab ano) {
   
        if (time == ano.time)   return p < ano.p;
        return time < ano.time;
    }

    bool operator== (Crontab ano) {
   
        return time == ano.time && cmd == ano.cmd;
    }
    
};
// 用于给答案进行排序的比较函数
bool cmp(Crontab a, Crontab b) {
   
    if (a.time == b.time)   return a.p < b.p;
    return a.time < b.time;
}

vector
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值