Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Palindrome Date Calculation

Tech 1

Palindrome Date Calculation

The code below checks for palindrome dates between two given date strings.

#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
string data1;
string data2;
int cnt;

int y1,m1,d1;
int y2,m2,d2;

int day[] = {31,28,31,30,31,30,31,31,30,31,30,31};
bool check(string s)
{
    int i=0, j=s.size()-1;
    while(i<j)
    {
        if(s[i]!=s[j])  return false;
        i++;    j--;
    }

    return true;
}
void digitalize()
{
    y1 = stoi(data1.substr(0,4));
    m1 = stoi(data1.substr(4,2));
    d1 = stoi(data1.substr(6,2));

    y2 = stoi(data2.substr(0,4));
    m2 = stoi(data2.substr(4,2));
    d2 = stoi(data2.substr(6,2));
}
void addoneday()
{

    int flag = 0;   //判断是否是闰年
    if(y1%400==0 || ((y1%4)==0&&(y1%100)!=0))   flag = 1;

    if(flag && m1==2)
    {
        if(d1==29)
        {
            m1=3;
            d1=1;
        }else
            d1++;
    }else
    {
        if(d1==day[m1-1])
        {
            d1=1;
            m1++;
            if(m1==13)  {m1=1;  y1++;
        }else
            d1++;
    }
}
int main()
{

    cin >> data1;
    cin >> data2;
    digitalize();

    int k =0;
    while(1)
    {
        if(y1==y2&&m1==m2&&d1==d2)  break;

        string s = to_string(y1);
        if(m1<10)   s = s + "0" + to_string(m1);
        else    s = s + to_string(m1);
        if(d1<10)   s = s + "0" + to_string(d1);
        else    s = s + to_string(d1);

        if(check(s))    cnt++;
        addoneday();

    }
    if(check(data2))    cnt++;

    cout << cnt << endl;
    return 0;
}

The following version passes all test cases:

#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
string data1;
string data2;
int cnt;

int y1,m1,d1;
int y2,m2,d2;

int day[] = {31,28,31,30,31,30,31,31,30,31,30,31};
bool check(string s)
{
    int i=0, j=s.size()-1;
    while(i<j)
    {
        if(s[i]!=s[j])  return false;
        i++;    j--;
    }

    return true;
}
void digitalize()
{
    y1 = stoi(data1.substr(0,4));
    m1 = stoi(data1.substr(4,2));
    d1 = stoi(data1.substr(6,2));

    y2 = stoi(data2.substr(0,4));
    m2 = stoi(data2.substr(4,2));
    d2 = stoi(data2.substr(6,2));
}
void addoneday()
{

    int flag = 0;   //判断是否是闰年
    if(y1%400==0 || ((y1%4)==0&&(y1%100)!=0))   flag = 1;

    if(flag && m1==2)
    {
        if(d1==29)
        {
            m1=3;
            d1=1;
        }else
            d1++;
    }else
    {
        if(d1==day[m1-1])
        {
            d1=1;
            m1++;
            if(m1==13)  {m1=1;  y1++;
        }else
            d1++;
    }
}
int main()
{

    cin >> data1;
    cin >> data2;
    digitalize();

    int k =0;
    while(1)
    {
        if(y1>y2)  break;

        int st = y1%10*10 + y1%100/10;
        if(st==0||st>12)  {y1++;m1=1;d1=1;continue;}    //年份
        if(m1<st)  {m1=st;d1=1;}    // 月份
        if(m1>st)   {y1++;continue;}

        if(y1==y2 && (m1>m2||(m1==m2&&d1>=d2)))  break;

        string s = to_string(y1);
        if(m1<10)   s = s + "0" + to_string(m1);
        else    s = s + to_string(m1);
        if(d1<10)   s = s + "0" + to_string(d1);
        else    s = s + to_string(d1);


        if(check(s))    cnt++;
        addoneday();

    }
    if(check(data2))    cnt++;

    cout << cnt << endl;
    return 0;
}

Analysis: This is a problem based on logical thinking.

Reasons for the long time taken: Total of one hour and twenty minutes, too slow, I have other problems to solve. Mainly unfamiliar with to_string and stoi, and unclear logic. Initially, it was checking one day at a time, which caused timeout. Then, after observing that the palindrome implies a specific year, the month is determined by the reverse of the year's last two digits. It can be simplified further since only certain years like 10, 20, 30, etc., produce palindromes. How ever, the solution passed 90% of the test cases, so I made minor adjustments to the original code and spent 20 minutes debugging boundary conditions.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.