"Palindrome Date" ====================================== topic description
In daily life, the three elements of the year, month, and day can represent a unique date.
Cattle and cows are accustomed to a 8-digit number representing a date, where the first 4 digits represent the year, the next 2 digits represent the month, and the last 2 represent the date. Obviously: a date has only one representation method, and two different date representations will not be the same.
Niuniu that a date is a palindrome, when and only if the 8-digit number that represents the date is a palindrome. Now, Niu Niu wants to know: In his designated two dates between the two dates themselves, how many of the actual date of existence is palindrome.
A 8-digit number is a palindrome, when and only if for all I (1 <=i<= 8) The number of numbers from left to right and the first 9-i number (that is, the number of numbers from right to left) are the same.
For example:
For November 19, 2016, the 8-digit number 20161119 indicates that it is not a palindrome.
For January 2, 2010, the 8-digit 20100102 indicates that it is a palindrome.
For October 2, 2010, the 8-digit number 20101002 indicates that it is not a palindrome.
There are 12 months in each year:
Among them, 1, 3, 5, 7, 8, 10, December each month has 31 days, 4, 6, 9, November each month has 30 days, and for February, leap year has 29 days, common year 28 days.
A year is a leap years when and only if it meets one of the following two scenarios:
1. This year is an integer multiple of 4, but not an integral multiple of 100;
2. This year is an integer multiple of 400.
For example:
? The following years are a leap year: 2000, 2012, 2016.
The following several years are common year: 1900, 2011, 2014.
Input/output format
Input format:
The input consists of two lines, each containing a 8-digit number.
The first line indicates the starting date specified by the bull.
The second line indicates the end date specified by the Ox and ox.
Ensure that date_i and both are true dates, and that the year part must be 4 digits, and the first number is not 0.
Guarantee Date1-not later than Date2.
Output format:
The output line contains an integer that indicates how many dates are palindrome between Date1 and Date2.
====================================== ideas:
When the first four digits of the 8-bit palindrome (i.e., the year) are determined, the last four bits (i.e., the month, the day) must also be determined.
You can therefore enumerate the years and generate corresponding palindrome numbers.
If the number meets the criteria (is the correct date and within the start and end date), then ans+1.
Code:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 intdate1,date2,md1,md2,year1,year2,ans=0;5 6 BOOLRightintYearintMonthintDate//determine if the generated date is real7 {8 if((month> A)|| (month<1))return 0;9 if((date<1)|| (date> to))return 0;Ten if(month==2) One { A if(date> in)return 0; - if(date== in) - if(! ((year%4==0) && (year% -!=0)|| (year% -==0))) the return 0; - } - Else - if((month==4)|| (month==6)|| (month==9)|| (month== One)) + if(date== to)return 0; - return 1; + } A at intMain () - { - //freopen ("testdata.in", "R", stdin); - //freopen ("P2010.out", "w", stdout); - -Cin>>date1; inCin>>Date2; -year1=date1/10000; md1=date1%10000; toyear2=date2/10000; md2=date2%10000; + //Date: Year + month + day (8 digits), MD: Month + day (4 digits) -ans=0; the for(intI=YEAR1; i<=year2; i++) * { $ //enumerate years, and generate palindrome datesPanax Notoginseng inta1=i%Ten; - intA2= (i% -)/Ten; the intM=a1*Ten+A2;//m: Month + intA3= (i% +)/ -; A inta4=i/ +; the intd=a3*Ten+A4;//D: Day + intmd=m* -+D; - if(Right (I,M,D))//determine if the generated date is real $ { $ intdate=i*10000+MD; - if(DATE1<=DATE&&DATE<=DATE2) ans++; - } the } -cout<<ans;Wuyi}
View Code
"2016NOIP Universal" Palindrome Date (c + +)