abhispra
BAN USERFound the above question on glass door. The solution I could think is -
1>Find the paths to the nodes
2>Turns into a problem of finding the common node of two linked list.
3>Use the standard technique of finding the length and then finding the common point.
Can anybody confirm if this approach is fine. Also any other solution with better time complexity?? Appreciate the help.
#include "stdafx.h"
void ProcessYear(int nYear);
void ProcessMonth(int nMonth, int nYear);
void ProcessDay(int nDay, int nMonth, int nYear);
bool Palindrome(char* szDate);
int _tmain(int argc, _TCHAR* argv[])
{
int nStartYear = 0, nEndYear = 0;
printf("Enter the starting year\n");
scanf("%d", &nStartYear );
printf("Enter the Ending year\n");
scanf("%d", &nEndYear );
if((nStartYear > nEndYear) ||
(nStartYear <= 0) ||
(nEndYear <= 0))
{
printf("Incorrect input");
}
else
{
while(nStartYear <= nEndYear)
{
ProcessYear(nStartYear);
nStartYear++;
}
}
return 0;
}
void ProcessYear(int nYear)
{
for(int nMonth = 1; nMonth <= 12; nMonth++)
{
ProcessMonth(nMonth, nYear);
}
}
void ProcessMonth(int nMonth, int nYear)
{
bool bLeapYear = !(nYear / 4);
int nDays = 0;
switch(nMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
nDays = 31;
break;
case 2:
if(bLeapYear)
{
nDays = 29;
}
else
{
nDays = 28;
}
break;
default:
nDays = 30;
break;
}
for(int nDay = 1; nDay <= nDays; nDay++)
{
ProcessDay(nDay, nMonth, nYear);
}
}
void ProcessDay(int nDay, int nMonth, int nYear)
{
char szDate[256] = {0};
char szMonth[128] = {0};
char szDay[128] = {0};
if(nDay < 10 )
{
sprintf(szDay, "0%d", nDay);
}
else
{
sprintf(szDay, "%d", nDay);
}
if(nMonth < 10 )
{
sprintf(szMonth, "0%d", nMonth);
}
else
{
sprintf(szMonth, "%d", nMonth);
}
sprintf(szDate, "%s%s%d", szDay, szMonth, nYear);
if(true == Palindrome(szDate))
{
printf("%s/%s/%d\n", szDay, szMonth, nYear);
}
}
bool Palindrome(char* szDate)
{
bool bRet =true;
char* szTemp = szDate;
int nLength = 0;
while(*szTemp++ != '\0')
{
nLength++;
}
for(int nIndex = 0; nIndex <= (nLength / 2); nIndex++)
{
if(*(szDate + nIndex) != *(szDate + nLength - nIndex - 1))
{
bRet = false;
}
}
return bRet;
}
Just figured out after Googling, that it a standard ancestor problem.
- abhispra February 18, 2010