| Project Euler, Project 4 |
[Apr. 4th, 2009|07:59 pm] |
http://projecteuler.net/index.php?section=problems&id=4
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int biggest = 0;
int start = 100;
int end = 999;
void Prepend(char x, char* number, int c)
{
int length = c;
while (length > 0)
{
number[length] = number[length - 1];
length--;
}
number[0] = x;
number[c + 1] = '\0';
}
int IntToString(int number, char* str)
{
char n[20] = "0123456789";
int c = 0;
int y = number;
while (y)
{
Prepend(n[abs(y % 10)], str, c);
y /= 10;
c++;
}
return c;
}
int IsPalindrome(int p)
{
char nmbr[20];
int LastChar = IntToString(p, nmbr) - 1;
int FrstChar = 0;
int palindrome = 1;
while (LastChar >= FrstChar)
{
if (nmbr[LastChar] != nmbr[FrstChar])
{
palindrome = 0;
break;
}
LastChar--;
FrstChar++;
}
return palindrome;
}
int main()
{
for (int da = start; da <= end; da++)
{
for (int db = start; db <= end; db++)
{
int product = da * db;
if (IsPalindrome(product) == 1)
{
if (product > biggest)
{
biggest = product;
}
}
}
}
cout << "Biggest palindrome of two 3-digit numbers is: "
<< biggest << endl;
return 0;
}
Took a little longer than I would have liked thanks to a Terminator break I took after getting frustrated with the cat getting in my way. I don't care if they're the best way to detect killer robots, I hate pets. Also, I figured out how to rig my own itoa() thanks to this page. Also, GIP! |
|
|
| Comments: |
That. That. That. that. that. that. that. ... | |