蓝桥杯java运行超时_第八届蓝桥杯 java B组 日期问题

import java.util.Arrays;

import java.util.Calendar;

import java.util.Scanner;

class Main{

static boolean leap(int l)

{

if((l%4==0&&l%100!=0)||l%400==0)

return true;

return false;

}

public static void main(String[] args) {

Scanner in =new Scanner( System.in);

String s=in.nextLine();

int a=Integer.parseInt(s.substring(0, 2));

int b=Integer.parseInt(s.substring(3,5));

int c=Integer.parseInt(s.substring(6,8));

int[]z=new int[6];

z[0]=(2000+a)*10000+b*100+c;

z[1]=(1900+a)*10000+b*100+c;

z[2]=(2000+c)*10000+a*100+b;

z[3]=(2000+c)*10000+b*100+a;

z[4]=(1900+c)*10000+a*100+b;

z[5]=(1900+c)*10000+b*100+a;

int daycount[]={31,29,31,30,31,30,31,31,30,31,30,31};

Arrays.sort(z);

for(int i=0;i<6;i++)

{

int year=z[i]/10000;

int month=z[i]%10000/100;

int day=z[i]%100;

if(year<1960||year>2059)continue;

if(month<1||month>12)continue;

if(leap(year))daycount[1]=29;

else daycount[1]=28;

if(day<1||day>daycount[month-1])continue;

StringBuffer string=new StringBuffer(z[i]+"");

string.insert(4, '-');

string.insert(7, '-');

System.out.println(string);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用线程来实现代码超时功能。具体实现方式如下: 1. 创建一个线程,用于执行需要设定超时的代码块。 2. 在线程内部,使用Java自带的定时器(Timer)来设置超时时间。 3. 在超时时间到达时,通过抛出异常的方式中断正在执行的代码块。 以下是代码示例: ```java public class TimeoutThread extends Thread { private long timeout; // 超时时间 private boolean isTimeout = false; // 是否超时 private Runnable runnable; // 需要执行的代码块 public TimeoutThread(long timeout, Runnable runnable) { this.timeout = timeout; this.runnable = runnable; } @Override public void run() { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { isTimeout = true; TimeoutThread.this.interrupt(); // 中断正在执行的代码块 } }, timeout); try { runnable.run(); } catch (InterruptedException e) { if (isTimeout) { System.err.println("Code is timed out!"); } else { System.err.println("Code is interrupted!"); } } finally { timer.cancel(); } } } ``` 以上代码创建了一个TimeoutThread类,使用Timer定时器来实现超时功能。使用示例如下: ```java public static void main(String[] args) { TimeoutThread thread = new TimeoutThread(5000, () -> { // 在5秒内执行完毕 for (int i = 0; i < 10; i++) { System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } }); thread.start(); } ``` 以上示例会在5秒内打印出0到9,如果超过5秒还没有执行完毕,则会抛出InterruptedException异常并输出"Code is timed out!"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值