Time limit:2000msSingle Point time limit:1000msMemory Limit:256MB
Describe
Given two dates, calculate the number of February 29 (including the start date) between the two dates.
Only leap years have February 29, with a year that meets one of the following conditions:
1. The year can be divisible by 4 but not divisible by 100
2. The year can be divisible by 400
Input
The first behavior is an integer t that represents the number of data groups.
Each group of data then contains two rows. Each line is formatted as "Month day, Year", which represents a date. Month is {"January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "November", "Dec A string in the Ember "}. Day and year are two numbers.
The data guarantees that a given date is valid and that the first date is earlier than or equal to the second date.
Output
For each set of data output line, the shape is "case #X: Y". X is the number of data groups, starting with 1, Y is the answer.
Data range
1≤t≤550
Small Data:
2000≤year≤3000
Big Data:
2000≤year≤2x109
-
-
Sample input
-
-
4January, 2012March, 2012August, 2899August, 2901August, 2000August, 2005February, 2004February 29, 2 012
-
-
Sample output
-
-
Case #1:1Case #2:0Case #3:1Case #4:3
Take 400 as the period
Import Java.util.hashmap;import Java.util.map;import Java.util.scanner;public class Main {class date{public Date (int Day, int month, int year) {this.day = day; This.month = month; This.year = year; } int day,month,year; } String month[] = {"January", "February", "March", "April", "may", "June", "July", "August", "September", "October", " November "," December "}; map<string,integer> mapmonth = new hashmap<> (); private void Initmonth () {for (int i=0;i<month.length;i++) {mapmonth.put (month[i], i+1); }} private int coutnum (String start,string end) {Date St = date (start); Date ed = date (end); int res = 0; for (int i= (st.year-1)/4+1) *4;i<=ed.year/4*4;i+=4) {if (i%400==0) {res + = (ed.year/400-i/400) *97; res++; i = ed.year/400*400; }else{if (Isrun (i)) res++; }} if (Isrun (st.year)) {if (st.month>2) {res--; }} if (Isrun (ed.year)) {if (ed.MONTH<2) {res--; }else if (ed.month==2) {if (Ed.day <) res--; }} return res; } private Boolean Isrun (int year) {if ((year%4==0&&year%100!=0) | | | (year%400==0)) return true; return false; Private date Date (String str) {string s[] = Str.split (""); int month = Mapmonth.get (s[0]); int year = Integer.parseint (s[2]); int day = Integer.parseint (S[1].substring ("0,s[1].indexof ("))); return new Date (day, month, year); public static void Main (String args[]) {Scanner sc = new Scanner (system.in);/* Scanner sc = null; try {sc = new Scanner (New FileInputStream ("C:\\users\\ddguo\\desktop\\input.txt")); } catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }*/int num = Sc.nextint (); Sc.nextline (); Main main = new main (); Main.initmonth (); int casenum = 1; while (num-->0) {String start = Sc.nextline (); String end = Sc.nextlinE (); String res = "Case #" +casenum+ ":" +main.coutnum (start, end); System.out.println (RES); casenum++; } sc.close (); }}
Programming Beauty 2015 Qualifying Title 1: February 29