【蓝桥杯】回文日期

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/*
 * 回文时间
 */
public class Main {

	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);		
		String str = in.nextLine();
		
		huiWenDate(str);
		

	}
	
	public static void huiWenDate(String str) throws Exception{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");		
		date = sdf.parse(str);
		
		int flag1 = 0;
		int flag2 = 0;
		
		while(true) {
//			date.setDate(date.getDate() + 1);
			date.setTime(date.getTime() + 24*60*60*1000);
			
			if(huiWen(sdf.format(date)) && flag1 == 0) {
				System.out.println(sdf.format(date));			
				flag1 = 1;
			}
			
			if(ABHuiWen(sdf.format(date)) && flag2 == 0) {
				System.out.println(sdf.format(date));
				flag2 = 1;
			}
			
			if(flag1 == 1 && flag2 == 1) {
				break;
			}
		}
			
	}
	
	public static boolean ABHuiWen(String str) {
		if(str.charAt(0) == str.charAt(2) && str.charAt(5) == str.charAt(7) && str.charAt(0) == str.charAt(5)) {
			if(str.charAt(1) == str.charAt(3) && str.charAt(4) == str.charAt(6) && str.charAt(1) == str.charAt(4)) {
				return true;
			}		
		}	
		return false;
		
	}
	

	public static boolean huiWen(String str) {
		
		int i = 0;
		int j = str.length() - 1;
		
		while(i <= j) {
			if(str.charAt(i) != str.charAt(j)) {
				return false;
			}
			i++;
			j--;
		}
		
		return true;
	}

}

上一篇:正则表达式匹配-动态规划


下一篇:python查看一条语句的执行效率和字节码