闰日闰年问题 大年份计算
来源:互联网 发布:unity3d opencv 编辑:程序博客网 时间:2024/05/02 02:06
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
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
Note
The names of the months are {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" and "December"}. And the numbers of days for the months are {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31} respectively in a non-leap year. In a leap year, number of days for February is 29 days; others are same as shown in previous line.
#include <iostream>#include <string>#include <cstdio>#include <fstream>using namespace std;int count(int x,int y){ int n = y/4-y/100+y/400-((x-1)/4-(x-1)/100+(x-1)/400); return n;}int main(){ freopen("in.txt","r",stdin); string mon[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int T,t=0; int x,y,y1,y2,a1,a2; char str1[100],str2[100]; scanf("%d",&T); while(T--) { getchar(); scanf("%s%d, %d",str1,&a1,&y1); scanf("%s%d, %d",str2,&a2,&y2); int i,j; for(i=0;i<12;i++)//判断月份 if(mon[i]==str1) break; for(j=0;j<12;j++) if(mon[j]==str2) break; if(i+1>2)//如果开始时间大于2月29日,则直接将x设为开始时间的下一年 x=y1+1; else x=y1; if(j+1>2)//如果结束时间小于2月29日,则直接将y设为结束时间的前一年 y=y2; else if(j==1&&a2==29) y=y2; else y=y2-1; int ans=count(x,y); printf("Case %d: %d\n",++t,ans); }}
- 闰日闰年问题 大年份计算
- 闰年的计算问题
- 一行代码判断闰年和计算某月份天数
- 计算某年份是否为闰年以及总计过了几个闰年
- 闰年计算
- 闰年计算
- 闰年计算
- 判断某年份是否为闰年
- 闰年问题
- Javascript 计算闰年
- 用shell计算闰年
- 计算闰年代码
- 闰年的计算
- 计算闰年平年
- 闰年的计算
- Javascript计算闰年
- 闰年的计算
- 闰年的计算
- Ribbon界面开发
- 利用js给li标签添加序号
- java.util.ConcurrentModificationException
- 用Quickfix详解Fix(二)--运行源码
- Oracle同义词创建及其作用
- 闰日闰年问题 大年份计算
- 【7gyy】笔者支招:救出Win 7菜单附件“迷路”的工具
- 进程死锁及解决方案
- POJ 2184 Cow Exhibition
- 整理一些常见的java及android面试题(1)
- weinre安装及使用
- fn函数
- CentOs 配置网卡
- DB2创建新用户及授权研究