martes, 10 de abril de 2012

Project Euler 36

#include <iostream>
#include <string>
using namespace std;
bool esPalindrome(string cad)
{
    int i,j;
    i = 0;
    j = cad.size()-1;
    while(i<j)
        if(cad[i++]!=cad[j--])
            return false;
    return true;
}
string abase2(int n)
{
    string sol = "";
    while(n>0)
        sol = (char)((n&1)+'0')+sol , n >>= 1;
    return sol;
}
string aString(int n)
{
    string sol = "";
    while(n>0)
    {
        int d = n%10;
        sol = (char)(d+'0')+sol;
        n /= 10;
    }
 
    return sol;
}
int main()
{
//872187
    int suma = 0 ;
    const int n = 1000000;
    for(int i = 1; i < n; i+=2)
        if(esPalindrome(aString(i)) && esPalindrome(abase2(i)))
            suma += i;
    cout << suma << endl;
    return 0;
}

No hay comentarios:

Publicar un comentario