题解:
找到 最近的一次回文日期 和 ABABBABA的回文日期 当找到 ABABBABA的回文日期时返回退出循环返回答案。
(回文判断用遍历字符串的 charAt(i) 与 charAt(长度 - i) 是否相等,如果相等则使 判断指针加一,当判断指针与字符串长度相等时,则它为回文式;ABABBABA的判断则是 charAt(0) == charAt(2) && charAt(1) == charAt(3) 且回文 则它为ABABBABA回文式)
代码抄的蓝桥题库的答案:
import java.util.Scanner;
public class PalindromeDate1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int date = input.nextInt();
int month = date / 100 % 100;
int day = date % 100;
//判断日期是否符合条件
if((month > 0 && month <= 12) && (day > 0 && day <= 31))
outPutTwoDate(date);
}
//给定一个8位数的日期,找到该日期之后 下一个回文日期 和 下一个ABABBABA型的回文日期。
private static void outPutTwoDate(int date){
int ordinaryAns = 0,abAns = 0;
boolean flag = true;
while(true){
date = runTime(date);
//取离输入值最近的一次回文
if(flag && isPalindrome(date)){
ordinaryAns = date;
flag = false;
}
//ABAB 且 回文,则为 ABABBABA,当找到 ABABBABA 的回文日期则退出循环
if(isPalindrome(date) && isAB(date)){
abAns = date;
break;
}
}
//打印结果
System.out.println(ordinaryAns);
System.out.println(abAns);
}
//进行年月日增加
private static int runTime(int numDate){
//获取年月日
int year = numDate / 10000,month = (numDate / 100) % 100, day = numDate % 100;
//进行年月日增加
++day;
if(day > 31){
day = 1;
++month;
if(month > 12){
month = 1;
++year;
}
}
//返回增加后的年月日
return year * 10000 + month * 100 + day;
}
//判断是否回文
private static boolean isPalindrome(int receiveDate){
String strDate = receiveDate +"";
int strLen = strDate.length();
int isP = 0;
for(int i = 0; i < strLen; ++i){
if(strDate.charAt(i) == strDate.charAt(strLen - 1 - i)){
++isP;
}
}
// System.out.println(isP);
return isP == strLen;
}
//判断是否为 ABAB 类型
private static boolean isAB(int numDate){
String date = numDate + "";
return date.charAt(0) == date.charAt(2) && date.charAt(1) == date.charAt(3);
}
}



