February 29 --容斥原理
来源:互联网 发布:淘宝客微信群避免封号 编辑:程序博客网 时间:2024/05/03 13:24
It is 2012, and it's a leap year. So there is a "February 29" in this year, which is called leap day. Interesting thing is the infant who will born in this February 29, will get his/her birthday again in 2016, which is another leap year. So February 29 only exists in leap years. Does leap year comes in every 4 years? Years that are divisible by 4 are leap years, but years that are divisible by 100 are not leap years, unless they are divisible by 400 in which case they are leap years.
In this problem, you will be given two different date. You have to find the number of leap days in between them.
Input starts with an integer T (≤ 550), denoting the number of test cases.
Each of the test cases will have two lines. First line represents the first date and second line represents the second date. Note that, the second date will not represent a date which arrives earlier than the first date. The dates will be in this format - "month day, year", See sample input for exact format. You are guaranteed that dates will be valid and the year will be in between 2 * 103 to 2 * 109. For your convenience, the month list and the number of days per months are given below. You can assume that all the given dates will be a valid date.
For each case, print the case number and the number of leap days in between two given dates (inclusive).
4
January 12, 2012
March 19, 2012
August 12, 2899
August 12, 2901
August 12, 2000
August 12, 2005
February 29, 2004
February 29, 2012
Case 1: 1
Case 2: 0
Case 3: 1
Case 4: 3
分析:
暴力超时,用数学的容斥原理,分别计算从0到开始年份和结束年份的满足的润日数,设为b-a,那么包含的是
包含的是不包括起始年份但是包括结束年份的润日数,接下来就是再判断起始,如果是闰年并且月份小于等于
2,加一;如果结束年份不是闰年无所谓,如果是,那么只有月份大于2时或者月份等于2并且日子大于等于29才
满足条件,否则就要减一,因为b-a区间包括了结束年份。上代码:
/*容斥原理: * A∪B = A+B-A∩B; * 暴力枚举会超时, * (year%4==0&&year%100!=0)||year%400==0 * 上面这句话容斥原理的条件,画图即可理解 * */import java.util.*; public class Main{ static Scanner in = new Scanner(System.in); static boolean judgeYear(long year){ return (year%4==0&&year%100!=0)||year%400==0; } public static void main(String args[]){ String[] months = {"","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December"}; Map<String,Integer> moth = new HashMap<>(); for (int i = 1; i <= 12; i++) { moth.put(months[i], i); } int k=in.nextInt(); int ca = 0; long cnt; while(k-->0){ ca++; cnt = 0; String m1 = in.next(); String d1 = in.next(); long y1 = in.nextLong(); String m2 = in.next(); String d2 = in.next(); long y2 = in.nextLong(); cnt = ((y2/4-y2/100+y2/400)-(y1/4-y1/100+y1/400)); if(judgeYear(y1)){ if(moth.get(m1)<=2) cnt++; } if(judgeYear(y2)){ int d = Integer.valueOf(d2.substring(0, 2)); if(moth.get(m2)==1||(moth.get(m2)==2&&d<29)) cnt--; } System.out.println("Case "+ca+": "+cnt); } } }
- February 29 --容斥原理
- Light oj 1414 - February 29【容斥原理】
- 【LightOJ】1414 - February 29(容斥原理)
- lightoj-1414-February 29【容斥原理&&细节】
- 【Light OJ 1414】February 29(容斥原理)
- 【Light-oj】-1414 - February 29(容斥原理)
- Light 1414-February 29【容斥定理】
- February 29 容斥定理 求闰年个数
- Lightoj 1414 - February 29 (闰年统计,容斥定理)
- Light OJ 1414 February 容斥
- 容斥原理
- 容斥原理 POJ3904
- 【容斥原理】八
- 【八】容斥原理
- 广义容斥原理
- 容斥原理
- 容斥原理 专题
- 容斥原理 专题
- 计算机系统结构 学习笔记(浮点数据表示)
- git简单使用
- Google首推AI相机,技能是偷拍!
- “聚” 每逢佳节倍思亲,旅居海外和家人聚聚和朋友叙叙
- 拯救微积分学的基本定理
- February 29 --容斥原理
- 利用动态规划求解Fibonacci数列
- 201403_1相反数
- Windows10使用心得
- QT纯代码实现简单登录
- 神经网络要从原子抓起:原来白花花的银子才是神经芯片的未来
- Facebook 应用机器学习团队专访:人工智能在 Facebook 中的应用
- DOM动态创建标记实例
- Jquery学习前传