蓝桥杯 日期问题 Java 实现

这个题目应该有那么一点难吧可能是我太菜了,写了挺久还
题目: 题目OJ
在这里插入图片描述
(1)我们要注意这个东西还是有点坑的,首先这个年月日这东西,我们居然好像循环解决不了?要手动模拟? 有点麻烦
(2)我们输出的时候要按时间顺序输出,也就是我们得把这些时间排下序在输出
(3)然后我们就别想其他的了 我这个菜鸡感觉越想越复杂 直接暴力模拟吧 !
代码:

import java.util.*;
public class P7164日期问题 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String a[] = sc.next().split("/");
		int b[] = new int[3];
		for(int i = 0;i < a.length;i++)
			b[i] = Integer.valueOf(a[i]);
		TreeSet<yea> set = new TreeSet<yea>();
		if(b[0] <= 59){
			if(b[1] <= 12 && b[2] <= 31 && b[1] != 0 && b[2] !=0){
				yea ans = new yea();
				ans.y = b[0]+2000;
				ans.m = b[1];
				ans.d = b[2];
				set.add(ans);
			}
		}
		if(b[2] <= 59){
			if(b[0] <= 12 && b[1] <= 31 && b[1] != 0 && b[0] !=0){
				yea ans = new yea();
				ans.y = b[2]+2000;
				ans.m = b[0];
				ans.d = b[1];
				set.add(ans);
			}
			if(b[0] <= 31 && b[1] <= 12 && b[1] != 0 && b[0] !=0){
				yea ans = new yea();
				ans.y = b[2]+2000;
				ans.m = b[1];
				ans.d = b[0];
				set.add(ans);
			}
		}
		if(b[0] >= 60){
			if(b[1] <= 12 && b[2] <= 31 && b[1] != 0 && b[2] !=0){
				yea ans = new yea();
				ans.y = b[0]+1900;
				ans.m = b[1];
				ans.d = b[2];
				set.add(ans);
			}
		}
		if(b[2] >= 60){
			if(b[0] <= 12 && b[1] <= 31&& b[1] != 0 && b[0] !=0){
				yea ans = new yea();
				ans.y = b[2]+1900;
				ans.m = b[0];
				ans.d = b[1];
				set.add(ans);
			}
			if(b[1] <= 12 && b[0] <= 31&& b[1] != 0 && b[0] !=0){
				yea ans = new yea();
				ans.y = b[2]+1900;
				ans.m = b[1];
				ans.d = b[0];
				set.add(ans);
			}
		}
		for(yea t:set)
			System.out.printf("%d-%02d-%02d\n",t.y,t.m,t.d);
		sc.close();
	}
}
class yea implements Comparable{
	int y;
	int m;
	int d;
	public int compareTo(Object b){
		yea a = (yea)b;
		if(this.y > a.y)
			return 1;
		else if(this.y == a.y){
			if(this.m > a.m)
				return 1;
			else if(this.m == a.m){
				if(this.d > a.d)
					return 1;
				else return -1;
			}
			else
				return -1;
		}
		else return -1;
	}
}
发布了32 篇原创文章 · 获赞 5 · 访问量 862

猜你喜欢

转载自blog.csdn.net/shizhuba/article/details/105301073