Lightoj 1414 - February 29 (闰年统计,容斥定理)
来源:互联网 发布:社交网络的利弊 英文 编辑:程序博客网 时间:2024/05/17 06:04
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.
题意:
在给定的日期里面,统计出闰年的个数。
思路:
这就尴尬了,模拟好久卡死了,最后才发现用容斥定理,数论暂时没看到,这里参看了学长的博客。
关于容斥定理的讲解 -> 百度百科链接
AC CODE:
#include<stdio.h>#include<cstring>#include<map>/*-------------map映射的学习-----------------*/#include<string>#include<algorithm>#define HardBoy main()#define ForMyLove return 0;using namespace std;const int MYDD = 1103;char dd[16][32] = {"0", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};int Leap(int year) {//闰年的容斥return year/4 + year/400 - year/100 ;}int HardBoy {int tt, kc = 1;scanf("%d", &tt);map<string, int> month;for(int j = 1; j <= 12; j++) {month[dd[j]] = j;/* 月份单词映射为数字 */}while(tt--) {char kai[32], e[32];int m1, m2, d1, d2, y1, y2;scanf("%s %d, %d", kai, &d1, &y1);m1 = month[kai];scanf("%s %d, %d", e, &d2, &y2);m2 = month[e];if(m1 > 2)y1++;if(m2 < 2 || m2==2 && d2 < 29) y2--;int ans = Leap(y2) - Leap(y1-1);printf("Case %d: %d\n", kc++, ans);}ForMyLove}
- Lightoj 1414 - February 29 (闰年统计,容斥定理)
- February 29 容斥定理 求闰年个数
- 【LightOJ】1414 - February 29(容斥原理)
- Light 1414-February 29【容斥定理】
- lightoj-1414-February 29【容斥原理&&细节】
- Lightoj 1414 - February 29
- lightoj 1414 February 29
- 【Lightoj 1414 - February 29 】
- lightoj 1414 - February 29
- 【Light OJ 1414】February 29(容斥原理)
- 【Light-oj】-1414 - February 29(容斥原理)
- LightOj 1414 1414 - February 29
- LIGHTOJ 1414 - February 29【Leap】
- February 29 --容斥原理
- Light oj 1414 - February 29【容斥原理】
- LightOJ-1117 Helping Cicada(容斥定理)
- Light OJ 1414 February 容斥
- LightOJ - 1414 February 29 (日期模拟)有多少个2月29
- 快速排序
- python之sys.argv 用法解析
- HDU 1565 方格取数(1) 二分图最大点权独立集
- LeetCode 28Implement strStr() 寻找子字符串的下标的位置
- 【C语言】每瓶汽水1元,两个空瓶可以置换一瓶汽水,现在有20元,最多能喝多少瓶汽水。
- Lightoj 1414 - February 29 (闰年统计,容斥定理)
- LeetCode 398. Random Pick Index
- Poj3126 prime
- iphone,ipad各个版本的跑分,iphone7,iphone 8 pluse,ipad Pro,iphone4,iphone4s,ipad, iphone 6,iphone 6s,
- CNKI e study 去除word插件
- hysbz1008 越狱(组合数学)
- 两个矩阵相乘—Strassen算法(矩阵为偶数阶方阵)
- nodejs之处理表单数据
- Vijos P1056 图形面积