计算到某年为止的闰年数,其实很简单.设要计算的年为A,则到A年为止(含A年)的闰年数为: 闰年数=INT(A/4)-INT(A/100)+INT(A/400)这里:INT为取整数函数
#-*-coding:utf-8-*- ''' @author : liuhuiting Date : 2016年8月31日 Description :计算到某年为止的闰年数,其实很简单.设要计算的年为A,则到A年为止(含A年)的闰年数为: 闰年数=INT(A/4)-INT(A/100)+INT(A/400) 这里:INT为取整数函数 ''' import copy def is_leapyear(year): return ((year%4==0 and year%100!=0)or(year%400==0)) def calculate_year(year,mon,day): sum = year/4 - year/100 + year/400 if(is_leapyear(year)and(mon<2 or (mon==2 and int(day)<29))): sum -= 1 return sum 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'] res = 0 num = int(raw_input()) for i in range(num): year1 = raw_input() year2 = raw_input() year1_list,year2_list = split_day(year1, year2) for j in range(12): if(year1_list[0]==month[j]): mon1 = j+1 if(year2_list[0]==month[j]): mon2 = j+1 num1 = calculate_year(year1_list[2], mon1, year1_list[1]) num2 = calculate_year(year2_list[2], mon2, year2_list[1]) res = num2 - num1 if(is_leapyear(year1_list[2]) and mon1==2 and int(year1_list[1])==29): res += 1 print 'Case #%d: %d'%(i+1,res)