Lingt oj 1414 - February 29

题目链接:http://lightoj.com/volume_showproblem.php?problem=1414


1414 - February 29
Time Limit: 1 second(s)Memory Limit: 32 MB

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.


PROBLEM SETTER: MD. ARIFUZZAMAN ARIF
SPECIAL THANKS: JANE ALAM JAN

题目大意: 给你两个日期,算有多少的 2.29


解析:会用C++  STL map会简单些,还要懂一些算闰年的方法,如: y / 4 - y / 400  * 3 - (y % 400) / 100、 y / 4 - y / 100 + y / 400等,


不多说了,上代码:

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;

map<string, int> mp;
int check(int y)
{
    if(y / 400 == 0 || (y % 4 == 0 && y / 100 != 0))
        return 1;
    return 0;
}
int main()
{
    mp["January"] = 1, mp["February"] = 2, mp["March"] = 3, mp["April"] = 4, mp["May"] = 5, mp["June"] = 6;
    mp["July"] = 7, mp["August"] = 8, mp["September"] = 9, mp["October"] = 10, mp["November"] = 11, mp["December"] = 12;
    int t, cnt = 0;
    string s;
    int y1, m1, d1, y2, m2, d2;
    cin >> t;
    while(t--)
    {
        int t1 = 0, t2 = 0;
        char k[N];
        cin >> s >> d1; scanf(" %s", k); cin >> y1;  m1 = mp[s];
        cin >> s >> d2;  scanf(" %s", k); cin >> y2;  m2 = mp[s];
        if(check(y1))
        {
            if(m1 > 2)
                y1++;
        }
        y1--;
        t1 = y1 / 4 - y1 / 100 + y1 / 400;

        if(check(y2))
        {
            if(m2 > 2 || (m2 == 2 && d2 == 29))
               y2++;
        }
        y2--;
        t2 = y2 / 4 - y2 / 100 + y2 / 400;
        printf("Case %d: %d\n", ++cnt, t2 - t1);

    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
杭州电子科技大学在线评测系统(杭电OJ)中的题目1000-1100是一系列编程题,我将分别进行回答。 1000题是一个简单的入门题,要求计算两个整数的和。我们可以使用一个简单的算法,读取输入的两个整数,然后将它们相加,最后输出结果即可。 1001题是一个稍微复杂一些的题目,要求实现字符串的逆序输出。我们可以使用一个循环来逐个读取输入的字符,然后将这些字符存储在一个数组中。最后,我们可以倒序遍历数组并将字符依次输出,实现字符串的逆序输出。 1002题是一个求最大公约数的问题。我们可以使用辗转相除法来解决,即先求出两个数的余数,然后将被除数更新为除数,将除数更新为余数,直至两个数的余数为0。最后的被除数就是最大公约数。 1003题是一个比较简单的排序问题。我们可以使用冒泡排序算法来解决,即每次比较相邻的两个元素,如果它们的顺序错误就交换它们的位置。重复这个过程直至整个数组有序。 1100题是一个动态规划问题,要求计算给定序列中的最长上升子序列的长度。我们可以使用一个数组dp来保存到达每个位置的最长上升子序列的长度。每当遍历到一个位置时,我们可以将其和之前的位置比较,如果比之前位置的值大,则将其更新为之前位置的值加1,最后返回dp数组的最大值即可。 以上是对杭电OJ1000-1100题目的简要回答,涉及了一些基本的编程知识和算法思想。希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值