Skip to content

Instantly share code, notes, and snippets.

@pbassut
Created March 23, 2015 06:30
Show Gist options
  • Save pbassut/1303b1f0187b8c4521db to your computer and use it in GitHub Desktop.
Save pbassut/1303b1f0187b8c4521db to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
bool isTheGreatest(int x, int y){
static int greastest = 0;
if(x * y > greastest){
greastest = x * y;
cout << greastest << " Won!" << endl;
return true;
}
return false;
}
bool isPalindrome(int x, int y){
int result = x * y;
ostringstream convert;
convert << result;
string s = convert.str();
string half = s.substr(0, s.length() / 2);
int secondHalfIndex = s.length() / 2;
bool isOdd = s.length() % 2 == 1;
if(isOdd){
secondHalfIndex++;
}
string otherHalf = s.substr(secondHalfIndex);
std::reverse(otherHalf.begin(), otherHalf.end());
if(half.compare(otherHalf) == 0 && isTheGreatest(x, y)){
cout << x << " " << y << endl;
return true;
}
return false;
}
int main(){
int candidateX = 0, candidateY = 0;
for(int i = 999; i > 100; i--){
for(int j = 999; j > 100; j--){
if(isPalindrome(i, j)){
candidateX = i;
candidateY = j;
}
}
}
cout << "The numbers are " << candidateX << " and " << candidateY;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment