描述
给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。
只有闰年有2月29日,满足以下一个条件的年份为闰年:
1. 年份能被4整除但不能被100整除
2. 年份能被400整除
输入
第一行为一个整数T,表示数据组数。
之后每组数据包含两行。每一行格式为"month day, year",表示一个日期。month为{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}中的一个字符串。day与year为两个数字。
数据保证给定的日期合法且第一个日期早于或等于第二个日期。
输出
对于每组数据输出一行,形如"Case #X: Y"。X为数据组数,从1开始,Y为答案。
数据范围
1 ≤ T ≤ 550
小数据:
2000 ≤ year ≤ 3000
大数据:
2000 ≤ year ≤ 2×109
样例输入
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
描述
给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。
只有闰年有2月29日,满足以下一个条件的年份为闰年:
1. 年份能被4整除但不能被100整除
2. 年份能被400整除
输入
第一行为一个整数T,表示数据组数。
之后每组数据包含两行。每一行格式为"month day, year",表示一个日期。month为{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}中的一个字符串。day与year为两个数字。
数据保证给定的日期合法且第一个日期早于或等于第二个日期。
输出
对于每组数据输出一行,形如"Case #X: Y"。X为数据组数,从1开始,Y为答案。
数据范围
1 ≤ T ≤ 550
小数据:
2000 ≤ year ≤ 3000
大数据:
2000 ≤ year ≤ 2×109
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: 1Case #4: 3
下面是我的python 代码:提交一直提示WA(wrong answer),但试了很多组,并没有发现错误,求大神们指出:
#-*-coding:utf-8-*-
import copy
def judge_before(temp_day1,temp_day2,month,days):
if(temp_day1[2] < temp_day2[2]):
return True
else:
if(temp_day1[2] == temp_day2[2]):
if(month.index(temp_day1[0]) < month.index(temp_day2[0])):
return True
else:
if(month.index(temp_day1[0]) == month.index(temp_day2[0])):
if(int(temp_day1[1]) <= temp_day2[1]):
return True
else:
return False
else:
return False
else:
return False
#year1_list after year2_list
def judge_after(year1_list,year2_list,month,days):
if(year1_list[2]>year2_list[2]):
return True
else:
if(year1_list[2]==year2_list[2]):
if(month.index(year1_list[0])>month.index(year2_list[0])):
return True
else:
if(month.index(year1_list[0])==month.index(year2_list[0])):
if(int(year1_list[1])>int(year2_list[1])):
return True
else:
return False
else:
return False
else:
return False
return
def count_leapyear(year1_list,year2_list,month,days):
count = 0
year_start = year1_list[2]
year_end = year2_list[2]
year_judge = []
year_judge.append('February')
year_judge.append('29')
if((year_start == year_end) and((year_start %4 == 0 and year_start%100 != 0) or (year_start %400 ==0))):
year_judge.append(year_start)
if(judge_before(year1_list, year_judge, month, days)):
count = 1
return count
else:
return 0
for i in range(year_start+1,year_end):
if((i%4 == 0 and i%100 != 0) or (i%400 == 0)):
count += 1
if((year_start %4 == 0 and year_start%100 != 0) or (year_start %400 ==0)):
year_judge.append(year1_list[2])
if (judge_before(year1_list, year_judge, month, days)):
count += 1
if((year_end %4 == 0 and year_end%100 != 0) or (year_end %400 ==0)):
if(len(year_judge)==3):
year_judge.pop()
year_judge.append(year2_list[2])
else:
year_judge.append(year2_list[2])
if(judge_after(year2_list, year_judge, month, days)):
count += 1
return count
def legal_year(year_list,month,days,big_year):
if(year_list[2]>=2000 and year_list[2]<=big_year):
if((year_list[2]%4 == 0 and year_list[2]%100!=0)or(year_list[2]%400==0)):
if(year_list[0]=='February'):
if(int(year_list[1])<=29):
return True
else:
return False
else:
if(year_list[0] not in month):
return False
else:
if(int(year_list[1])<=days[month.index(year_list[0])]):
return True
else:
return False
else:
if(year_list[0] not in month):
return False
else:
if(int(year_list[1])<=days[month.index(year_list[0])]):
return True
else:
return False
else:
return False
def split_day(year1,year2):
year1_list = []
year2_list = []
temp_list = []
temp_list = copy.deepcopy(year1.split(','))
temp = temp_list[0].split()
temp.append(int(temp_list[1]))
year1_list = copy.deepcopy(temp)
temp_list = copy.deepcopy(year2.split(','))
temp = temp_list[0].split()
temp.append(int(temp_list[1]))
year2_list = copy.deepcopy(temp)
return year1_list,year2_list
if __name__ == '__main__':
month = ['January','February','March','April','May','June','July','August','September','October','November','December']
days = [31,28,31,30,31,30,31,31,30,31,30,31]
num = int(raw_input())
if(num>=1 and num<=550):
for i in range(num):
year1 = raw_input()
year2 = raw_input()
year1_list,year2_list = split_day(year1, year2)
if((legal_year(year1_list, month, days, 3000))and (legal_year(year2_list, month, days, 2000000000))):
if (judge_before(year1_list, year2_list,month,days)):
print 'Case #%i: %i' % (i+1,count_leapyear(year1_list, year2_list, month, days))
else:
break
else:
break