-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.cpp
77 lines (60 loc) · 1.49 KB
/
4.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
/*
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.
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include<vector>
using namespace std;
bool checkIfLargest(int N){
int Nc = N;
int factor = 2;
vector<int> factors;
while( N > 1){
if(N % factor == 0){
N = N / factor;
factors.push_back(factor);
while(N % factor == 0){
factors.push_back(factor);
N = N / factor;
}
}
factor = factor+1;
}
int a = 1;
int b = 1;
while(factors.size()>0){
int factor = factors.back();
if(a<=b && a*factor<1000){
a *= factor;
}else if(b*factor<1000){
b *= factor;
}else{
cout<<factor << " - " << a << " " << b << " " << Nc <<endl;
return false;
}
factors.pop_back();
}
return true;
}
bool checkIfValidPalindrome(string a){
int d = a.length();
return (a[0] == a[d-1]) && (a[1] == a[d-2]) && (a[2] == a[d-3]) ;
}
int main(void){
const int MAX = 999999;
int i;
for(i = MAX;i>10000; i--){
stringstream ss;
ss << i;
string s = ss.str();
if( checkIfValidPalindrome(s) && checkIfLargest(i) ){
break;
}
}
cout << i;
return 0;
}