历届真题 日期问题【第八届】【省赛】【B组】

以下是个人解析认知,拿不到满分,只能拿到75分,有大佬能看出问题来帮忙在评论区解答以下。

从题目中我们可以了解到,我们做这个题用不到太高深的算法,只需要简单的了解条件即可:

1.闰年判断,以及闰年中的2月的天数问题(因为只有2月天数会变嘛)

2.年数合法判断,保证在题目要求的年数范围内

3.月份判断,每个月的天数是否合法,月数是否越界

4.最后输出的值按顺序输出并且不能出现相同的

import java.util.*;
import java.io.*;
/*
 年/月/日
 月/日/年
 日/月/年。 
1960年1月1日   到   2059年12月31日
 */
public class Main {
	//要判断闰年
	public static void main(String [] args) throws Exception {
	       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	       String[] date = br.readLine().split("/");
	       String date1 = time1(date);
	       String date2 = time2(date);
	       String date3 = time3(date);
	       //先自动去重
	       HashSet<String> hs = new HashSet<>();
	       hs.add(date1);
	       hs.add(date2);
	       hs.add(date3);
	       
	       LinkedList<String> ls = new LinkedList<>();
	       for(String s : hs) {
	    	   if(!isTrue(s)) {
	 	    	      //答案不对
	 	 	          ls.add(s);
	 	       }
	       }
	       
	       hs.removeAll(ls);
	       //输出结果要排序
	       TreeSet<String> ts = new TreeSet<>(hs);
	       
	       for(String s : ts) {
	    	    System.out.println(s);
	       }	       
	}
	
	//判断
	static boolean isTrue(String s) {
		   String year = s.substring(0,4);
		   String month = s.substring(5,7);
		   String day = s.substring(8,10);
		   
		   if(day.charAt(0) == '0') {
			     if(day.charAt(1) == '0') return false;
			     day = ""+day.charAt(1);
		   }
		   
		   if(month.charAt(0) == '0') {
			     if(month.charAt(1) == '0') return false;
			     month = ""+month.charAt(1);
		   }
		   
		   if(Integer.parseInt(month) > 12) {
			   return false;
		   }
		   
		   //判断闰年和2月
		   if((Integer.parseInt(year) % 4 != 0 || Integer.parseInt(year) % 100 == 0
                   || Integer.parseInt(year) % 400 != 0) && Integer.parseInt(month) == 2 && 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")) && Integer.parseInt(day) > 31) {
			       return false;
		   }
		   
		   if((month.equals("4")||month.equals("6")||month.equals("9")||month.equals("11")) && Integer.parseInt(day) > 30) {
			   return false;
		   }
		   
		   return true;
	}

	
	//年-月-日
	  static String time1(String[] date) {
		   String year = date[0];
		   String month = date[1];
		   String day = date[2];
		   if(year.charAt(0) == '0') {
			    //证明肯定是20年的
			    year = "20" + year;
		        return year+"-"+month+"-"+day;
		   }
		   
		   //为20年时
		   if(Integer.parseInt(year) <= 59) {
			    year = "20" + year;
			    return year+"-"+month+"-"+day;
		   }else {
			   //证明为19年
			   year = "19" + year;
			   return year+"-"+month+"-"+day;
		   }
	  }
	
	//月-日-年
	  static String time2(String[] date) {
		   String month = date[0];
		   String day = date[1];
		   String year = date[2];
		   
		   if(year.charAt(0) == '0') {
			    //证明肯定是20年的
			    year = "20" + year;
		        return year+"-"+month+"-"+day;
		   }
		   
		   //为20年时
		   if(Integer.parseInt(year) <= 59) {
			    year = "20" + year;
                return year+"-"+month+"-"+day;
		   }else {
			   //证明为19年
			   year = "19" + year;
               return year+"-"+month+"-"+day;
		   }
	  }
	  
	//日-月-年
	  static String time3(String[] date) {
		   String day = date[0];
		   String month = date[1];
		   String year = date[2];
		   if(year.charAt(0) == '0') {
			    //证明肯定是20年的
			    year = "20" + year;
		        return year+"-"+month+"-"+day;
		   }
		   
		   //为20年时
		   if(Integer.parseInt(year) <= 59) {
			     year = "20" + year;
                 return year+"-"+month+"-"+day;
		   }else {
			   //证明为19年
			   year = "19" + year;
               return year+"-"+month+"-"+day;
		   }
	  }
	  
	  
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮卡丘学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值