ccf-20171203 Crontab问题
这题有如下几个点要注意:
1.最开始输出的开始时间和截止时间,这里是不包含截止时间的。
2.月份和星期的英文表示是大小写任意的,并未规定必须是Sat这种形式。
3.星期天的数字标识是0。
我的思路是,首先将月份、天数、小时、分钟、星期统统规格化,格式如下:
月份:规格化前:1,2-4
规格化后:1 2 3 4
再使用stringstream类来依次遍历规格化的字符串。在遍历月份的过程中,要考察天数与月份是否匹配,同时判定是否满足星期的要求。
最后判定结果是否在小于截止时间,并且大于等于起始时间。
题目和代码如下:
#include <bits/stdc++.h>
/*#include<stdio.h>
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<sstream>
#include<map>
#include<vector>*/
using namespace std;
class Time{
public:
int year,month,day,hour,minute;
Time(int year_,int month_,int day_,int hour_,int minute_):year(year_),month(month_),day(day_),hour(hour_),minute(minute_){
}
void ShowTime(){
cout<<year<<","<<month<<","<<day<<","<<hour<<","<<minute<<endl;
}
};
class Event{
public:
string min,hou,dayofmonth,mon,dayofweek,command;
Event(string min_,string hou_,string dayofmonth_,string mon_,string dayofweek_,string command_):min(min_),hou(hou_),dayofmonth(dayofmonth_),mon(mon_),dayofweek(dayofweek_),command(command_){
}
void ShowEvent(){
cout<<min<<","<<hou<<","<<dayofmonth<<","<<mon<<","<<dayofweek<<","<<command<<endl;
}
};
string Int_to_String(int n)
{
ostringstream stream;
stream<<n; //n为int类型
return stream.str();
}
string Operator_special(string temps){
string tts="";
stringstream ss(temps);
string temp="";
while(getline(ss,temp,',')){
if(temp.find("-")!=string::npos){
stringstream ss1(temp);
string temp_i="";
string temp_j="";
getline(ss1,temp_i,'-');
getline(ss1,temp_j,'-');
for(int i=atoi(temp_i.c_str());i<=atoi(temp_j.c_str());i++){
tts+=Int_to_String(i);
tts+=" ";
}
}else{
tts+=temp;
tts+=" ";
}
}
return tts;
} void toStandard(string &str)//转化为标准小写
{
int len=str.size();
for(int i=0;i<len;++i)str[i]=tolower(str[i]);
}
int main(){
map<string,vector<string> > mmps;
int n;
string s,t;
cin>>n>>s>>t;
Time * Total[2];
Total[0]=new Time(atoi(s.substr(0,4).c_str()),atoi(s.substr(4,2).c_str()),atoi(s.substr(6,2).c_str()),atoi(s.substr(8,2).c_str()),atoi(s.substr(10,2).c_str()));
Total[1]=new Time(atoi(t.substr(0,4).c_str()),atoi(t.substr(4,2).c_str()),atoi(t.substr(6,2).c_str()),atoi(t.substr(8,2).c_str()),atoi(t.substr(10,2).c_str()));
Event * event[n];
for(int i1=0;i1<n;i1++){
string temp_min,temp_hou,temp_dayofmonth,temp_mon,temp_dayofweek,command;
cin>>temp_min>>temp_hou>>temp_dayofmonth>>temp_mon>>temp_dayofweek>>command;
//event[i]=new Event(temp_min,temp_hou,temp_dayofmonth,temp_mon,temp_dayofweek,command);
string result="";
toStandard(temp_mon);//不区别大小写,转化为标准小写
map<string,int> months;
months["jan"]=1,months["feb"]=2,months["mar"]=3,months["apr"]=4,months["may"]=5,months["jun"]=6,months["jul"]=7,months["aug"]=8,months["sep"]=9,months["oct"]=10,months["nov"]=11,months["dec"]=12;
map<string,int>::iterator it;
for(it=months.begin();it!=months.end();it++){
if(temp_mon.find(it->first)!=string::npos){
temp_mon.replace(temp_mon.find(it->first),3,Int_to_String(it->second));
}
}
if(temp_mon=="*"){
temp_mon="";
for(int ii=1;ii<=12;ii++){
temp_mon+=Int_to_String(ii);
temp_mon+=" ";
}
//cout<<temp_mon<<endl;
}else if(temp_mon.find(",")!=string::npos||temp_mon.find("-")!=string::npos){
string tts=Operator_special(temp_mon);
temp_mon=tts;
}
if(temp_dayofmonth=="*"){
temp_dayofmonth="";
for(int ii=1;ii<=31;ii++){
temp_dayofmonth+=Int_to_String(ii);
temp_dayofmonth+=" ";
}
//cout<<temp_dayofmonth<<endl;
}else if(temp_dayofmonth.find(",")!=string::npos||temp_dayofmonth.find("-")!=string::npos){
string tts=Operator_special(temp_dayofmonth);
temp_dayofmonth=tts;
}
toStandard(temp_dayofweek);//不区别大小写,转化为标准小写
map<string,int> weeks;
weeks["sun"]=0,weeks["mon"]=1,weeks["tue"]=2,weeks["wed"]=3,weeks["thu"]=4,weeks["fri"]=5,weeks["sat"]=6;
//map<string,int>::iterator it;
for(it=weeks.begin();it!=weeks.end();it++){
if(temp_dayofweek.find(it->first)!=string::npos){
temp_dayofweek.replace(temp_dayofweek.find(it->first),3,Int_to_String(it->second));
}
}
if(temp_dayofweek.find(",")!=string::npos||temp_dayofweek.find("-")!=string::npos){
string tts=Operator_special(temp_dayofweek);
temp_dayofweek=tts;
}
if(temp_hou=="*"){
temp_hou="";
for(int ii=0;ii<=23;ii++){
temp_hou+=Int_to_String(ii);
temp_hou+=" ";
}
//cout<<temp_hou<<endl;
}else if(temp_hou.find(",")!=string::npos||temp_hou.find("-")!=string::npos){
string tts=Operator_special(temp_hou);
temp_hou=tts;
}
if(temp_min=="*"){
temp_min="";
for(int ii=0;ii<=59;ii++){
temp_min+=Int_to_String(ii);
temp_min+=" ";
}
}else if(temp_min.find(",")!=string::npos||temp_min.find("-")!=string::npos){
string tts=Operator_special(temp_min);
temp_min=tts;
}
string temps1="",temps2="",temps3="",temps4="";
for(int i=Total[0]->year;i<=Total[1]->year;i++){
int total_day0=0;
for(int ii=1970;ii<i;ii++)//Total会在外层再包含一个循环
{
if(ii%400==0||(ii%4==0&&ii%100!=0)){
total_day0+=366;
}else{
total_day0+=365;
}
}
istringstream inputString1(temp_mon);
while(inputString1>>temps1){
istringstream inputString2(temp_dayofmonth);
while(inputString2>>temps2){
int day_li=0;
if(atoi(temps1.c_str())==2){
if(i%400==0||(i%4==0&&i%100!=0)){
day_li=29;
}else{
day_li=28;
}
}else if((atoi(temps1.c_str())==1)||(atoi(temps1.c_str())==3)||(atoi(temps1.c_str())==5)||(atoi(temps1.c_str())==7)||(atoi(temps1.c_str())==8)||(atoi(temps1.c_str())==10)||(atoi(temps1.c_str())==12)){
day_li=31;
}else{
day_li=30;
}
/* if(atoi(temps2.c_str())==31){
cout<<"here"<<endl;
cout<<day_li<<endl;
}*/
if(atoi(temps2.c_str())>day_li){
continue;
}
int total_day=total_day0;
if(temp_dayofweek!="*"){
int ii=atoi(temps1.c_str());
int num2=28;
if(i%400==0||(i%4==0&&i%100!=0)){
num2++;
}
switch(ii){
case 12:total_day+=30;
case 11:total_day+=31;
case 10:total_day+=30;
case 9:total_day+=31;
case 8:total_day+=31;
case 7:total_day+=30;
case 6:total_day+=31;
case 5:total_day+=30;
case 4:total_day+=31;
case 3:total_day+=num2;
case 2:total_day+=31;
case 1:break;
}
total_day+=atoi(temps2.c_str());
total_day-=1;
int week=(total_day%7+4)%7;
/*if(command=="go_to_bed"&&(i==2018)){
cout<<"total_day:"<<total_day<<endl;
cout<<"week:"<<week<<endl;
cout<<"temp_dayofweek:"<<temp_dayofweek<<endl;
cout<<temps1<<temps2<<endl;
}*/
if(temp_dayofweek.find(Int_to_String(week))==string::npos){
continue;
}
}
istringstream inputString3(temp_hou);
while(inputString3>>temps3){
istringstream inputString4(temp_min);
while(inputString4>>temps4){
temps1=temps1.length()==1?(temps1="0"+temps1):temps1;
temps2=temps2.length()==1?(temps2="0"+temps2):temps2;
temps3=temps3.length()==1?(temps3="0"+temps3):temps3;
temps4=temps4.length()==1?(temps4="0"+temps4):temps4;
result=Int_to_String(i)+temps1+temps2+temps3+temps4;
if(result>=s&&result<t)
mmps[result].push_back(command);
}
}
}
}
}
}
for(map<string,vector<string> >::iterator it=mmps.begin();it!=mmps.end();++it)
{
map<string,int> isprt;
for(size_t i=0;i<it->second.size();++i)
{
string dis=it->first+" "+it->second[i];
if(isprt.count(dis)==0)
{
cout<<dis<<endl;
isprt[dis]=1;
}
}
}
return 0;
}
ccf-20171203 Crontab问题的更多相关文章
- ccf 201712-3 Crontab(Python实现)
一.原题 问题描述 试题编号: 201712-3 试题名称: Crontab 时间限制: 10.0s 内存限制: 256.0MB 问题描述: 样例输入 3 201711170032 201711222 ...
- 【CCF CSP】 20171203 行车路线 Java(有问题)80分
问题描述 小明和小芳出去乡村玩,小明负责开车,小芳来导航. 小芳将可能的道路分为大道和小道.大道比较好走,每走1公里小明会增加1的疲劳度.小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走s公 ...
- CCF认证历年试题
CCF认证历年试题 不加索引整理会死星人orz 第一题: CCF201712-1 最小差值(100分) CCF201709-1 打酱油(100分) CCF201703-1 分蛋糕(100分) CCF2 ...
- crontab介绍
1.Cron的启动与关闭 由于Cron是Linux的内置服务,可以用以下的方法启动.关闭这个服务: /sbin/service crond start //启动服务/sbin/se ...
- 使用python crontab设置linux定时任务
熟悉linux的朋友应该知道在linux中可以使用crontab设置定时任务.可以通过命令crontab -e编写任务.当然也可以直接写配置文件设置任务. 但是有时候希望通过脚本自动设置,比如我们应用 ...
- 浅谈 linux 例行性工作 crontab (linux定时任务)
定时任务大家都挺说过,就好比你手机上的闹钟,到了指定的时候就会响起. 今天在对redis缓存进行定时储存时又操作了一把,发现一些细节,写的不好.大家就将就看吧, 首先 简单介绍一下linux 例行性工 ...
- 用Crontab打造简易工作流引擎
1. 引言 众所周知,Oozie(1, 2)是基于时间条件与数据生成来做工作流调度的,但是Oozie的数据触发条件只支持HDFS路径,故而面临着这样的问题: 无法判断Hive partition是否已 ...
- Linux crontab定时器的使用
crontab参数: -u:帮助其他用户建立或移除工作排程 -l:查阅crontab的工作内容 -r:移除所有的crontab的工作内容 -e:编辑crontab文件 每项工作有六个字段: * * * ...
- crontab 启动 、运行 和编辑 查看
cron服务是Linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service crond stop /sbi ...
- Linux下使用crontab定时备份日志
上周学习了Linux,其中有使用crontab定时备份日志的内容,现把主要步骤记录如下: 首先需要备份的日志的源目录位于/opt/lampp/logs/access_log 备份到/tmp/logs下 ...
随机推荐
- IDEA中SpringBoot项目快速创建单元测试
如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...
- Django集成OpenLDAP认证
本文详细介绍了django-auth-ldap的使用方法,参数含义,并提供了示例代码 版本说明 Django==2.2 django-auth-ldap==1.7.0 集成过程 Django集成LDA ...
- GC频繁抖动的主要原因
内存抖动 内存抖动是因为大量的对象被创建又在短时间内马上被释放,如循环中分配对象,很容易引起GC,特别是在较大的循环次数或者一个循环中分配较多的临时对象时. 瞬间产生大量的对象 瞬间产生大量的对象,即 ...
- byte数组转float 以及byte转其他类型时为什么要&0xff
static final char[] HEX_CHARS = "0123456789abcdef".toCharArray(); //转换为十六进制 public static ...
- 深入理解苹果系统(Unicode)字符串的排序方法
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由iminder发表于云+社区专栏 Unicode编码 我们知道计算机是不能直接处理文本的,而是和数字打交道.因此,为了表示文本,就建立 ...
- Java基础之循环语句、条件语句、switch case 语句
Java 循环结构 - for, while 及 do...while 顺序结构的程序语句只能被执行一次.如果您想要同样的操作执行多次,,就需要使用循环结构. Java中有三种主要的循环结构: whi ...
- netty源码解解析(4.0)-3 Channel的抽象实现
AbstractChannel和AbstractUnsafe抽象类 io.netty.channel.AbstractChannel 从本章开始,会有大量的篇幅涉及到代码分析.为了能够清晰简洁的地说明 ...
- python集合操作和内置方法
一 集合基本介绍 集合:在{}内用逗号隔开每个值,集合的特点: 每个值必须是不可变类型 集合是无序的 集合的值不能重复 集合的应用场景较少,最重要的应用场景为进行关系运算以及去重. 二 集合的操作 1 ...
- 《Photoshop CS4手绘艺术技法》
书名 <Photoshop CS4手绘艺术技法> 图片 时间 2017-4月 学习 想了想当初的学习动机,自己P图片可是P的是实在是丑就会做几张动图.看完了才发现这行博大精深而且自己的审 ...
- [android] 手机卫士接收短信指令执行相应操作
通过广播接收者,接收到短信,对短信内容进行判断,如果为我们指定的值就执行相应的操作 如果短信内容是”#*location*#” 就执行,获取手机位置 如果短信内容是”#*alarm*#” 就执行,播放 ...