p04.cpp
1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
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);
*/