蓝桥杯 回文日期(Java)

 

解题思路

1.首先要满足回文的形式,即ABCDDCBA,对于回文的判断用数组会方便许多。

2.ABABBABA型:此处也可以使用数组判断是否是回文型。

3.要满足日期的格式。

暴力求解思路:预先存储好各个月份的天数。需要三个判断方法:

                         a.判断是否是闰年,如果是,则将存储的二月份的天数改为29

                         b.判断是否是回文型

                         c.判断是否是ABAB型

        通过三重for循环来寻找下一次回文日期。巧妙运用for循环的执行顺序就可以实现从当前日期往后开始寻找。先增加天数,天数满了增加月份,月份也满了增加年份。要注意增加月份和年份时对应地要初始化天数和月份。通过两个Boolean值来判断是否找到我们的目标日期,当两个Boolean值都为true时说明都找到了我们的目标日期,可以退出循环了。

代码

import java.util.*;
class Main{
	static boolean IsRun(int y) {
		if(y%400==0||(y%4==0&&y%100!=0)) return true;
		return false;
	}
	static boolean ABBA(String Str){//判断回文日期
		char []str=Str.toCharArray();
		if(str[0]==str[7]&&str[1]==str[6]&&str[2]==str[5]&&str[3]==str[4]) return true;
		return false;
	}
	static boolean ABAB(String Str) {//判断ABBA型
		char []str=Str.toCharArray();
		if(str[0]==str[2]&&str[2]==str[5]&&str[5]==str[7]&&str[1]==str[3]&&str[3]==str[4]&&str[4]==str[6]&&str[0]!=str[1]) 
			return true;
		return false;
	}
	public static void main(String args[]) {
		int monthes[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
		Scanner in=new Scanner(System.in);
		String time=in.next();
		Integer tt=Integer.parseInt(time);
		int year=tt/10000;
		int month=tt%10000/100;
		int day=tt%100+1;
		int n;
		int []ans=new int[2];
		boolean flag1=false,flag2=false;
		if(IsRun(year)) monthes[2]=29;
		for(int i=year;i<=9999;i++,month=1,day=1) {//巧妙运用for循环的顺序,可以实现从当前日期开始往后寻找
			for(int j=month;j<=12;j++,day=1) {
				for(int k=day;k<=monthes[j];k++) {
					n=i*10000+j*100+k;

					String Str=String.valueOf(n);
					if(ABBA(Str)&&flag1==false) {
						String str1=Str;
						flag1=true;
						ans[0]=n;
					}
					if(ABAB(Str)&&flag2==false) {
						String str2=Str;
						flag2=true;
						ans[1]=n;
					}
					if(flag1==true&&flag2==true) break;
				}
			}
		}
		System.out.println(ans[0]);
		System.out.println(ans[1]);
	}
}

总结反思

第一遍自己做的时候,想得过于复杂了,既要考虑是否符合日期,又要考虑若不符合下一次应该为哪个,没有想过直接暴力求解,循环增加时间来判断寻找目标日期,思路不清楚,虽然最后过了,但是逻辑很混乱,自己也是懵里懵懂的过的。

通过这道题加深我我对回文的认识,起初我对回文的判断是将年份切割为一位位int型数值然后比较,通过去网上寻找其他题解我了解了还可以用更简单的数组来判断回文,也感觉到了循环是寻找回文的一种比较简单明了的思路。

以下是第一遍做题的代码:(属于自己都不想看系列)

import java.util.*;
class Main{
	static boolean legal(Integer y,Integer rem,Integer red) {
		if(rem>0&&rem<=12) {
			if(rem==1||rem==3||rem==5||rem==7||rem==8||rem==10||rem==12) {
				if(red>0&&red<=31) return true; 
			}
			if(rem==4||rem==6||rem==9||rem==11) {
				if(red>0&&red<=30) return true;
			}
			if(rem==2) {
				if((y%4==0&&y%100!=0)||y%400==0) {
					if(red>0&&red<=29) return true;
				}
				else if(red>0&&red<=28) return true;	
			}
		}
		return false;
	}
	public static void main(String args[]) {
		Scanner in=new Scanner(System.in);
		String str=in.nextLine();//结束键为Enter
		Integer y=Integer.parseInt(str.substring(0, 4));//年
		Integer m=Integer.parseInt(str.substring(4, 6));//月
		Integer d=Integer.parseInt(str.substring(6,8));//日
		StringBuffer year=new StringBuffer(str.substring(0,4));
		StringBuffer ystr=new StringBuffer(year);
		ystr=ystr.reverse();
		year.append(ystr);
		Integer rem=Integer.parseInt(ystr.substring(0,2));
		Integer red=Integer.parseInt(ystr.substring(2,4));
		Integer yd=Integer.parseInt(str.substring(0,2));
	    Integer ym=Integer.parseInt(str.substring(2,4));
		if(legal(y,rem,red)==true) {
			if(m<rem) System.out.println(year);
			if(m>=rem) {
				if(d<red) System.out.println(year);
			}
		}
		else {
//		String str1=str.substring(0,4);//年份
//	    Integer mon=Integer.parseInt(str.substring(4,6));
//	    Integer day=Integer.parseInt(str.substring(6,8));
//		StringBuffer reyear=new StringBuffer(str1);
//		reyear=reyear.reverse();
//		Integer rem=Integer.parseInt(str1.substring(0,2));
//		Integer red=Integer.parseInt(str1.substring(2,4));
		StringBuffer cs;
		StringBuffer csc;
		//回文日期
		Integer d0=yd,m0=ym;
//	System.out.println("d0:"+d0+" m0:"+m0);
		
		if(red>31) {
		if(d0/10==9) d=1;
		if(d0/10==1) {
			if(d0<13) d0=d0+1;
			else d0=d0/10*10+10;
		}
		else if(d0%10<2) d0=d0+1;
		else d0=d0/10*10+10;
		m0=1;
		}
		else {
			if(m0==0||m0>=90) {
				m0=1;
			}
			else if(m0==20) m0=21;
			else m0=m0/10*10+10;
		}
		if(d0==1) csc=new StringBuffer("01");
		else csc=new StringBuffer(d0.toString());
		if(m0==1) csc=csc.append("01");
		else csc=csc.append(m0.toString());
	
        cs=new StringBuffer(csc);
        cs=cs.reverse();
        csc.append(cs);
		System.out.println(csc);
		}
		//ABABBABA
		if(yd%10==0&&ym%10==0&&((m<ym)||(m==ym&&d<yd))) {
			 System.out.println(year);
		}
		

		else {
		Integer a=yd/10*10;
		if(yd%10!=0||(yd%10==0&&ym>=yd)) {
			if(a==20) {
				a=21;
			}
			a=a+10;
	    }
		String s1=a.toString()+a.toString();
		StringBuffer AB=new StringBuffer(s1);
		AB=AB.reverse();
		System.out.println(s1+AB);
		}
	}
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值