World-wide symmetric daily computing (C ++)

Source: Internet
Author: User

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;
}
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.