CCF Crontab JAVA实现

import java.io.*;

import java.util.*;

class order{

public LinkedList<String> minutes=new LinkedList<String>();
public LinkedList<String> hours=new LinkedList<String>();
public LinkedList<String> day_month=new LinkedList<String>();
public LinkedList<String> month=new LinkedList<String>();
public LinkedList<String> day_week=new LinkedList<String>();
public String DoSth;
public  String begin_year;
public String end_year;
public LinkedList<String> minAndhour=new LinkedList<String>();
public LinkedList<String> dayAndyear=new LinkedList<String>();
public LinkedList<String> time=new LinkedList<String>();
//时间链,将所有符合条件的时间都add上去

order(String[] haha,String s,String t){
function(haha[0],haha[1],haha[2],haha[3],haha[4],haha[5]);
this.begin_year=s;
this.end_year=t;
sort(minutes);
sort(hours);
sort(day_month);
sort(month);
sort(day_week);
this.set_minAndhou();
this.set_dayAndyear();
this.set_time();//生成时间链
}
//生成年月日链
private void set_dayAndyear() {
// TODO Auto-generated method stub
for (int i = Integer.parseInt(this.begin_year.substring(0, 4)); i <=Integer.parseInt(this.end_year.substring(0, 4)) ; i++) {
for (int j = 0; j < this.month.size(); j++) {
for (int j2 = 0; j2 < this.day_month.size(); j2++) {
String vv=i+this.month.get(j).toString()+this.day_month.get(j2).toString();
//判断年月日链是否合法
if(check(i,Integer.parseInt(this.month.get(j)),Integer.parseInt(this.day_month.get(j2)),this.day_week)
&&vv.compareTo(begin_year.substring(0, 8))>=0&&vv.compareTo(end_year.substring(0, 8))<=0){
this.dayAndyear.add(vv);
}
}
}
}
}
private boolean check(int temp_year, int temp_month, int temp_day, LinkedList<String> day_week2) {
// TODO Auto-generated method stub
int num_year=0;
int num_month=0;
int num_day=temp_day;
if (((temp_day%4==0)&&(temp_day%100!=0))||(temp_day%400==0)) {
switch (temp_month){
case 5:num_month=0;
break;
case 8:num_month=1;
break;
case 2:num_month=1;
break;
case 3:num_month=2;
break;
case 1:num_month=5;
break;
case 4:num_month=5;
break;
case 6:num_month=3;
break;
case 7:num_month=5;
break;
case 9:num_month=4;
break;
case 10:num_month=6;
break;
case 11:num_month=2;
break;
case 12:num_month=4;
break;
}
}else{
switch (temp_month){
case 5:num_month=0;
break;
case 8:num_month=1;
break;
case 2:num_month=2;
break;
case 3:num_month=2;
break;
case 1:num_month=6;
break;
case 4:num_month=5;
break;
case 6:num_month=3;
break;
case 7:num_month=5;
break;
case 9:num_month=4;
break;
case 10:num_month=6;
break;
case 11:num_month=2;
break;
case 12:num_month=4;
break;
}
}
num_year=((temp_year%100)/4+temp_year%100)%7;
if (day_week2.contains(String.valueOf((num_year+num_month+num_day)%7))) {//************
return true;
} else {
return false;
}

}
//生成小时分钟链
private void set_minAndhou() {
// TODO Auto-generated method stub
for (int i = 0; i < this.hours.size(); i++) {
for (int j = 0; j < this.minutes.size(); j++) {
this.minAndhour.add(this.hours.get(i).toString()+this.minutes.get(j).toString());
}
}
}
//排序
private void sort(LinkedList<String> minutes2) {
// TODO Auto-generated method stub
for (int i = 0; i < minutes2.size(); i++) {
for (int j = 0; j < minutes2.size()-i-1; j++) {
if (Integer.parseInt(minutes2.get(j))>Integer.parseInt(minutes2.get(j+1))) {
int temp=Integer.parseInt(minutes2.remove(j+1));
//minutes2.add(j, temp);
minutes2.add(j, String.valueOf(temp));
}
}
}
}
//将适当的值传给order
private void function(String min, String hou, String d_m, String mon, String d_w, String haha) {
// TODO Auto-generated method stub

fun_min(min);
fun_hou(hou);
fun_d_m(d_m);
fun_mon(mon);
fun_d_w(d_w);
fun_haha(haha);
}
//处理命令
private void fun_haha(String haha) {
// TODO Auto-generated method stub
this.DoSth=haha;
}
//处理星期
private void fun_d_w(String d_w) {
// TODO Auto-generated method stub
d_w=d_w.toLowerCase();
d_w=d_w.replaceAll("sun", "0");
d_w=d_w.replaceAll("mon", "1");
d_w=d_w.replaceAll("tue", "2");
d_w=d_w.replaceAll("wed", "3");
d_w=d_w.replaceAll("thu", "4");
d_w=d_w.replaceAll("fri", "5");
d_w=d_w.replaceAll("sat", "6");
if (d_w.contains("*")) {
for (int i = 0; i <= 6; i++) {
this.day_week.add(String.valueOf(i));
}
return;
} else if( d_w.contains(",")){//两种情况,包含-和,   或者包含只包含,
String[] str=d_w.split(",");
for (int i = 0; i < str.length; i++) {
if (!str[i].contains("-")) {
this.day_week.add(str[i]);
}else{
String[] temp=str[i].split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
this.day_week.add(String.valueOf(j));
}
}
}
return;
}else if(d_w.contains("-")){//只包含-
String[] temp=d_w.split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
this.day_week.add(String.valueOf(j));
}
return;
}else{
this.day_week.add(d_w);
return;
}
}
//处理月份
private void fun_mon(String mon) {
// TODO Auto-generated method stub
//处理月份
//先将字母转换为小写
//然后将月份转换为对应的月份
mon=mon=mon.toLowerCase();
mon=mon.replaceAll("jan","1" );
mon=mon.replaceAll("feb","2" );
mon=mon.replaceAll("mar","3" );
mon=mon.replaceAll("apr","4" );
mon=mon.replaceAll("may","5" );
mon=mon.replaceAll("jun","6" );
mon=mon.replaceAll("jul","7" );
mon=mon.replaceAll("aug","8" );
mon=mon.replaceAll("sep","9" );
mon=mon.replaceAll("oct","10" );
mon=mon.replaceAll("nov","11" );
mon=mon.replaceAll("dec","12" );
if (mon.contains("*")) {
for (int i = 1; i <= 12; i++) {
if (i<10) {
this.month.add("0"+String.valueOf(i));
} else {
this.month.add(String.valueOf(i));
}

}
return;
} else if(mon.contains(",")){//两种情况,包含-和,   或者包含只包含,
String[] str=mon.split(",");
for (int i = 0; i < str.length; i++) {
if (!str[i].contains("-")) {
if (Integer.parseInt(str[i])<10) {
this.month.add("0"+str[i]);
} else {
this.month.add(str[i]);
}

}else{
String[] temp=str[i].split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {

if (j<10) {
this.month.add("0"+String.valueOf(j));
} else {
this.month.add(String.valueOf(j));
}
}
}
}
return;
}else if(mon.contains("-")){//只包含-
String[] temp=mon.split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
if (j<10) {
this.month.add("0"+String.valueOf(j));
} else {
this.month.add(String.valueOf(j));
}
}
return;
}else{

if (Integer.parseInt(mon)<10) {
this.month.add("0"+mon);
} else {
this.month.add(mon);
}
return;
}
}
//处理天
private void fun_d_m(String d_m) {
// TODO Auto-generated method stub
if (d_m.contains("*")) {
for (int i = 1; i <= 31; i++) {
if (i<10) {
this.day_month.add("0"+String.valueOf(i));
} else {
this.day_month.add(String.valueOf(i));
}

}
return;
} else if(d_m.contains(",")){//两种情况,包含-和,   或者包含只包含,
String[] str=d_m.split(",");
for (int i = 0; i < str.length; i++) {
if (!str[i].contains("-")) {
if (Integer.parseInt(str[i])<10) {
this.day_month.add("0"+str[i]);
} else {
this.day_month.add(str[i]);
}

}else{
String[] temp=str[i].split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {

if (j<10) {
this.day_month.add("0"+String.valueOf(j));
} else {
this.day_month.add(String.valueOf(j));
}
}
}
}
return;
}else if(d_m.contains("-")){//只包含-
String[] temp=d_m.split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
if (j<10) {
this.day_month.add("0"+String.valueOf(j));
} else {
this.day_month.add(String.valueOf(j));
}
}
return;
}else{
if (Integer.parseInt(d_m)<10) {
this.day_month.add("0"+d_m);
} else {
this.day_month.add(d_m);
}

return;
}
}
//处理小时
private void fun_hou(String hou) {
// TODO Auto-generated method stub
if (hou.contains("*")) {
for (int i = 0; i < 24; i++) {
if (i<10) {
this.hours.add("0"+String.valueOf(i));
} else {
this.hours.add(String.valueOf(i));
}

}
return;
} else if(hou.contains(",")){//两种情况,包含-和,   或者包含只包含,
String[] str=hou.split(",");
for (int i = 0; i < str.length; i++) {
if (!str[i].contains("-")) {
if (Integer.parseInt(str[i])<10) {
this.hours.add("0"+str[i]);
} else {
this.hours.add(str[i]);
}

}else{
String[] temp=str[i].split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
if (j<10) {
this.hours.add("0"+String.valueOf(j));
} else {
this.hours.add(String.valueOf(j));
}
}
}
}
return;
}else if(hou.contains("-")){//只包含-
String[] temp=hou.split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
if (j<10) {
this.hours.add("0"+String.valueOf(j));
} else {
this.hours.add(String.valueOf(j));
}
}
return;
}else{
if (Integer.parseInt(hou)<10) {
this.hours.add("0"+hou);
} else {
this.hours.add(hou);
}
return;
}
}
//处理分钟
private void fun_min(String min) {
// TODO Auto-generated method stub
if (min.contains("*")) {
for (int i = 0; i < 60; i++) {
if (i<=0) {
this.minutes.add("0"+String.valueOf(i));
}else
this.minutes.add(String.valueOf(i));
}
return;
} else if(min.contains(",")){//两种情况,包含-和,   或者包含只包含,
String[] str=min.split(",");
for (int i = 0; i < str.length; i++) {
if (!str[i].contains("-")) {
if (Integer.parseInt(str[i])<10) {
this.minutes.add("0"+str[i]);
} else {
this.minutes.add(str[i]);
}
}else{
String[] temp=str[i].split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
if (j<10) {
this.minutes.add("0"+String.valueOf(j));
} else {
this.minutes.add(String.valueOf(j));
}
//this.minutes.add(String.valueOf(j));
}
}
}
return;
}else if(min.contains("-")){//只包含-
String[] temp=min.split("-");
for (int j = Integer.parseInt(temp[0]); j <= Integer.parseInt(temp[1]); j++) {
if (j<10) {
this.minutes.add("0"+String.valueOf(j));
} else {
this.minutes.add(String.valueOf(j));
}
//
}
return;
}else{
if (Integer.parseInt(min)<10) {
minutes.add("0"+min);
} else {
minutes.add(min);
}
return;
}
}

public void set_time(){//生成时间链,时间从小到大
for (int i = 0; i < this.dayAndyear.size(); i++) {
for (int j = 0; j < this.minAndhour.size(); j++) {
String temp=this.dayAndyear.get(i)+this.minAndhour.get(j);
if (temp.compareTo(begin_year)>0&&temp.compareTo(end_year)<0) {
this.time.add(temp);
}
}
}
}
}
public class Main{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int n=in.nextInt();//命令个数
Long s=in.nextLong();//开始时间
Long t=in.nextLong();//结束时间 
order[] ordergroup=new order[n];
in.nextLine();
for (int i = 0; i < n; i++) 
{
String ss=in.nextLine();
String[] temp=ss.split(" ");
ordergroup[i]=new order(temp,s.toString(),t.toString());
}
//截止现在,每个order都生成了时间链,现在,按时间顺序由小到大输出
//整个功能写在end函数中
end(ordergroup);

}


private static void end(order[] ordergroup) {
// TODO Auto-generated method stub
while(true){
//每次比较链头,取最小值 
int temp=timemin(ordergroup);//返回最小值的链表的序号
输出并去掉链头
System.out.println(ordergroup[temp].time.removeFirst()+" "+ordergroup[temp].DoSth);
//判断所有时间是否输出完
if(isKong(ordergroup)){
break;
}
}
}
private static int timemin(order[] ordergroup) {
// TODO Auto-generated method stub
String timeout="210000000000";
int i=0;
for (int j = 0; j < ordergroup.length; j++) {
if (ordergroup[j].time.isEmpty()) {
continue;
}
if (ordergroup[j].time.get(0).compareTo(timeout)<0) {
timeout=ordergroup[j].time.get(0);
i=j;
}
}
return i;
}
private static boolean isKong(order[] ordergroup) {
// TODO Auto-generated method stub
for (int i = 0; i < ordergroup.length; i++) {
if (!(ordergroup[i].time.isEmpty())) {
return false;
}
}
return true;
}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值