-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes4.cpp
executable file
·105 lines (96 loc) · 2.25 KB
/
es4.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<iostream>
using namespace std;
int lung(int num){
if (!num){
return 0;
}
if (num/10 == 0){
return 1;
}
int lunghezza = lung(num/10);
return 1+lunghezza;
}
/*
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.
*/
void numeri(int num, int * A){
int lunghezza = lung(num) - 1;
int const lung = lunghezza;
int num1 = num;
int num2 = 0;
while(lunghezza >= 0){
num2 = num1/10;
num2 = num2*10;
num2 = num1-num2;
num1 = num1/10;
A[lunghezza] = num2;
lunghezza--;
}
}
int reverse(int * A, int dim){
if (dim%2 != 0){
return -1;
}
int newNum = 0;
for (int i = 0; i < dim; i++){
int addNum = A[dim-i-1];
int cont = dim-i-1;
while(cont){
addNum = addNum*10;
cont--;
}
newNum += addNum;
}
return newNum;
}
int pal(int num){
if (lung(num)%2==0){
int const lung_num = lung(num);
int A[lung_num];
numeri(num, A);
int reverse_num = reverse(A, lung_num);
if (num == reverse_num){
return true;
}
}
return false;
}
int F(int n_digit){
n_digit = n_digit - 1;
int cont = n_digit;
int num1 = 9;
while (cont){
int cont2 = cont;
int addNum = 9;
while(cont2){
addNum = addNum*10;
cont2--;
}
num1 += addNum;
cont--;
}
//cout << num1;
int num2 = num1, iniziale = num1, result = 0;
bool found = false;
int max = -1;
while(lung(num1) > lung(iniziale/10)){
result = num1*num2;
//cout << result << endl;
found = pal(result);
if (found && result > max){
max = result;
}
if (lung(iniziale/10) > lung(num2)){
cout << num1 <<endl;
num1--;
num2 = iniziale;
}
num2--;
}
return max;
}
int main(){
cout << endl << F(3) << endl;
system("pause");
}