Project Euler Problem 36 Double-base palindromes

Double-base palindromes

Problem 36

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)


C++(Simpler):

#include 

using namespace std;

const int N = 1000000;

bool ispalindrom(int n, int base)
{
    int palindrom = 0, temp;

    temp = n;
    while(temp) {
        palindrom *= base;
        palindrom += temp % base;
        temp /= base;
    }

    return n == palindrom;
}

int main()
{
    long total = 0;

    for(int i=1; i


C++:

#include 
#include 

using namespace std;

const int N = 1000000;

void myitoa(int n, char *p, int base)
{
    while(n) {
        *p++ = '0' + n % base;
        n /= base;
    }

    *p = '\0';
}

bool ispalindrom(char s[])
{
    int start = 0;
    int end = strlen(s) - 1;

    while(start < end) {
        if(s[end] != s[start])
            return false;
        start++;
        end--;

    }

    return true;
}

int main()
{
    char s[32];
    long total = 0;

    for(int i=1; i



你可能感兴趣的:(#,Project,Euler问题程序解)