Today is the world's fully symmetric Day (2011 1102), so I want to write an algorithm to calculate all the fully symmetric days in a period of time. Let's see how many days are the world's fully symmetric day.
Description:
Enter the start and end years to obtain all the fully symmetric dates.
Input:
Enter the start year and end year endyear (startyear <endyear );
Output:
The exact symmetric day required by the output.
Solution:
1) Calculate the date of the corresponding year based on the month and day in reverse order (for example, in the case of January 1-> 0101-> 1010, the year is 1010)
2) Determine whether the calculated year is between the input year
3) exclude the illegal date of January 1, February 29 when a non-leap year is exceeded.
Code:
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
#include <algorithm>
using namespace std;
const int MonthDays[] ={
31,29,31,30,31,30,31,31,30,31,30,31
};
class SymmetricalDay
{
public:
bool isLeap(int year)
{
return (( year % 4 == 0 ) && ( year % 100 != 0 ) || ( year % 400 == 0 ));
}
vector<string> getDays(int startYear, int endYear)
{
vector<string> results;
for (int curMonth = 1; curMonth <= 12; ++curMonth)
{
for (int curDay = 1; curDay <= MonthDays[curMonth-1]; ++curDay)
{
ostringstream tempValue;
tempValue << setw(2) << setfill('0') << curMonth;
tempValue << setw(2) << setfill('0') << curDay;
string strData(tempValue.str());
string strReverse(strData.rbegin(), strData.rend());
istringstream yearValue(strReverse);
int curYear = 0;
yearValue >> curYear;
if (curYear >= startYear && curYear <= endYear)
{
if (!isLeap(curYear) && curMonth==2 && curDay==29)
{
continue;
}
string tempResult = yearValue.str() + "" + tempValue.str();
results.push_back(tempResult);
}
}
}
sort(results.begin(), results.end());
return results;
}
};