p04.cpp 1.52 KB
/*
Largest palindrome product
Problem 4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

By:dbk
*/
#include <stdio.h>
#include <string.h>

using namespace std;

#define MAX 10000

bool validPalindromic(int);
bool validPalindromicApuntador(int);

int main(int argc, char const *argv[])
{

	int a = 1;
	int b = 1;

	int c = 0;

	int res = 0;

	do
	{
		a = 1;
		do
		{
			c = a * b;
			res = (validPalindromicApuntador(c) && c > res) ? c : res;

		}while(a++ < MAX);
	}while(b++ < MAX);
	printf("Resultado final: %d\n", res);
	return 0;
}


bool validPalindromic(int valueCheck)
{
	bool flagValid = true;
	char buffer[30];
	int counterDsc = 0;
	int counterAsc = 0;
	int sizeStringNumber = 0;

	//Convierte el entero a una cadena
	sprintf(buffer, "%d", valueCheck);
	counterDsc = sizeStringNumber = strlen(buffer);
	while(sizeStringNumber > counterAsc) {
		flagValid &= (buffer[counterAsc++] == buffer[--counterDsc]) ? true : false;
	}

	return flagValid;
}

bool validPalindromicApuntador(int valueCheck)
{
	char *ptrAsc;
	char *ptrDsc;
	char buffer[30];
	bool flagValid = true;

	//Convierte el entero a una cadena
	sprintf(buffer, "%d", valueCheck);
	ptrAsc = buffer;
	ptrDsc = &buffer[strlen(buffer)];

	while(ptrDsc >= ptrAsc) {
		flagValid &= (*(ptrAsc++) == *(--ptrDsc));
	}
	return flagValid;
}

/*
	TEST DIR MEM Apuntadores
	printf("mem1:\t%p\n", ptrAsc);
	printf("mem2:\t%p\n", ptrDsc);
*/