February 29 容斥定理 求闰年个数
2015-07-28 11:08
260 查看
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.
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.
题意:给一个时间区间 求在该区间内有几个闰年
前n年有多少个闰年:num(n)=n/4-n/100+n/400
n~m年有多少闰年 :num(m)-num(n-1);
注意当n的月份>2月29 时,n要++;
当m的月份《2月29时,m要--;
code:
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
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.
Output
For each case, print the case number and the number of leap days in between two given dates (inclusive).Sample Input | Output for Sample Input |
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 |
前n年有多少个闰年:num(n)=n/4-n/100+n/400
n~m年有多少闰年 :num(m)-num(n-1);
注意当n的月份>2月29 时,n要++;
当m的月份《2月29时,m要--;
code:
#include<iostream> #include<algorithm> #include<stdio.h> #include<map> #include<string> using namespace std; map<string, int>ma; void init() { ma["January"]=1; ma["February"]=2; ma["March"]=3; ma["April"]=4; ma["May"]=5; ma["June"]=6; ma["July"]=7; ma["August"]=8; ma["September"]=9; ma["October"]=10; ma["November"]=11; ma["December"]=12; } int main() { //freopen("i.txt","r",stdin); init(); int cas,cas1=0; int t1,t2,y1,y2; int m1,m2; string s1,s2; scanf("%d",&cas); while(cas--) { cas1++; cin>>s1;scanf("%d,%d",&t1,&y1); cin>>s2;scanf("%d,%d",&t2,&y2); m1=ma[s1];m2=ma[s2]; if(m1>=3) y1++; y1--; if((m2<2) ||(m2==2 && t2<29)) y2--; int tem1=y1/4-y1/100+y1/400; int tem2=y2/4-y2/100+y2/400; printf("Case %d: ",cas1); printf("%d\n",tem2-tem1); } return 0; }
相关文章推荐
- seaJs学习笔记2 – seaJs组建库的使用
- HTML5基础 --- Canvas元素
- JSP中的include动态加载
- Swap Nodes in Pairs
- javascript_this的用法
- HTML5实现摇一摇
- js数组的操作
- CSS中background-position的使用
- CSS实现文字自动换行
- 初识HTML
- html的三种样式定义方式:外部样式表;内部样式表;内联样式表
- JSON.parse()和JSON.stringify()
- EXC_BREAKPOINT(code=EXC_ARM_BREAKPOINT,subcode=0xdefe)
- jquery获取select框的值、单选框的值、textarea的值、获取input框的值
- JSON.stringify 语法实例讲解
- 深入学习JavaScript中的Rest参数和参数默认值
- JQuery实现鼠标滚轮滑动到页面节点
- 延迟加载外部js文件
- 延迟加载外部js文件
- CSS强制英文、中文换行与不换行 强制英文换行