因为在网上看到很多的回文日期的博客作者都是用个数,十位数,百位数...这种做的
本篇文章是字符串做法,比较好理解嘛\
这种题的做法很简单,跟我上条的博客一样,主要是考虑问题比较多
废话不多说,上代码
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int date = Integer.parseInt(br.readLine());
for(int i = date+1;i<100000000;i++) {
String s = String.valueOf(i);
//判断是否合法
if(!isTrue(s)) {
continue;
}
if(s.charAt(0) == s.charAt(7) && s.charAt(1) == s.charAt(6) &&
s.charAt(2) == s.charAt(5) && s.charAt(3) == s.charAt(4)) {
//如果是回文子串
System.out.println(s);
break;
}
}
for(int i = date+1;i<100000000;i++) {
String s = String.valueOf(i);
if(!isTrue(s)) {
continue;
}
if(s.charAt(0) == s.charAt(7) && s.charAt(1) == s.charAt(6) &&
s.charAt(2) == s.charAt(5) && s.charAt(3) == s.charAt(4)) {
//如果是回文子串
if(s.charAt(0) == s.charAt(2) && s.charAt(1) == s.charAt(3)) {
System.out.println(s);
break;
}
}
}
}
static boolean isTrue(String s) {
String year = s.substring(0,4);
String month = s.substring(4,6);
String day = s.substring(6,8);
if(Integer.parseInt(month) > 12 || Integer.parseInt(month) < 1) {
return false;
}
//如果该年份不是闰年
if(Integer.parseInt(year) % 4 != 0 || Integer.parseInt(year) % 400 != 0
|| Integer.parseInt(year) % 100 == 0 ) {
if(month.equals("2")) {
if(Integer.parseInt(day) > 28) {
return false;
}
}
if(month.equals("1") || month.equals("3") || month.equals("5") ||
month.equals("7") || month.equals("8") || month.equals("10") ||
month.equals("12")) {
if(Integer.parseInt(day) > 31) {
return false;
}
}
if(Integer.parseInt(day) > 30) {
return false;
}
}
//证明该年份是闰年
if(month.equals("2")){
if(Integer.parseInt("day") > 29) {
return false;
}
}
if(month.equals("1") || month.equals("3") || month.equals("5") ||
month.equals("7") || month.equals("8") || month.equals("10") ||
month.equals("12")) {
if(Integer.parseInt(day) > 31) {
return false;
}
}
if(Integer.parseInt(day) > 30) {
return false;
}
return true;
}
}