和中国的节日不同,美国的节假日通常是选择某个月的第几个星期几这种形式,因此每一年的放假日期都不相同。具体规则如下:
* 1月1日:元旦
* 1月的第三个星期一:马丁·路德·金纪念日
* 2月的第三个星期一:总统节
* 5月的最后一个星期一:阵亡将士纪念日
* 7月4日:美国国庆
* 9月的第一个星期一:劳动节
* 11月的第四个星期四:感恩节
* 12月25日:圣诞节
现在给出一个年份,请你帮忙生成当年节日的日期。
输入包含多组数据,每组数据包含一个正整数year(2000≤year≤9999)。
对应每一组数据,以“YYYY-MM-DD”格式输出当年所有的节日日期,每个日期占一行。
每组数据之后输出一个空行作为分隔。
2014 2013
2014-01-01 2014-01-20 2014-02-17 2014-05-26 2014-07-04 2014-09-01 2014-11-27 2014-12-25 2013-01-01 2013-01-21 2013-02-18 2013-05-27 2013-07-04 2013-09-02 2013-11-28 2013-12-25
import java.util.*; public class Main{ public static final int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //判断是否为闰年 public static boolean isLeap(int n){ return (((n % 4 == 0 && n % 100 != 0) || n % 400 == 0)); } //给定M-Y-D,返回这一年一共过了多少天 public static int nDays(int y,int m,int d){ int n = d; for(int i = 0;i < m - 1;i++){ n += arr[i]; } if(m > 2 && isLeap(y)){ n++; } return n; } //给定M-Y-D,找到从公元前1年12月31日开始过了过久。就出它%7得t同余数 public static int diff(int y,int m,int d){ return (y - 1)+(y - 1)/4 - (y - 1)/100 + (y -1)/400 + nDays(y,m,d); } //根据y-m-d,求出星期几 public static int week(int y,int m,int d){ int w = diff(y, m, d) % 7; if(w == 0){ return 7; } return w; } //根据1号是星期几,求第n个星期e是几号 public static int m1(int w,int n,int e){ return 1 + (n - 1) * 7 + (7 - w + e) % 7; } //根据6月1号星期w,求5月的最后一个星期一 public static int m2(int w){ int d = (w == 1 ? 7 : w - 1); return 32 - d; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int y = sc.nextInt(); /* 1月1日:元旦 * 1月的第三个星期一:马丁·路德·金纪念日 * 2月的第三个星期一:总统节 * 5月的最后一个星期一:阵亡将士纪念日 * 7月4日:美国国庆 * 9月的第一个星期一:劳动节 * 11月的第四个星期四:感恩节 * 12月25日:圣诞节 */ System.out.printf("%d-01-01\n",y); int w; w = week(y,1,1); System.out.printf("%d-01-%02d\n",y,m1(w,3,1)); w = week(y,2,1); System.out.printf("%d-02-%02d\n",y,m1(w,3,1)); w = week(y,6,1); System.out.printf("%d-05-%02d\n",y,m2(w)); System.out.printf("%d-07-04\n",y); w = week(y,9,1); System.out.printf("%d-09-%02d\n",y,m1(w,1,1)); w = week(y,11,1); System.out.printf("%d-11-%02d\n",y,m1(w,4,4)); System.out.printf("%d-12-25\n",y); System.out.println(); } } }