http://projecteuler.net/problem=32 [Answer:45228]

来源:互联网 发布:网络主播小可爱 编辑:程序博客网 时间:2024/04/29 16:10
#include <iostream>#include <vector>#include <algorithm>using namespace std;bool IsPandigital( int a, int b, int c ){    if ( c < 1000 || c >= 10000 )    {        return false;    }    bool bExist[10] = { true, false, false, false, false, false, false, false, false, false };    while ( a != 0 )    {        int remiander = a % 10;        if ( bExist[remiander] )        {            return false;        }        bExist[remiander] = true;        a /= 10;    }    while ( b != 0 )    {        int remiander = b % 10;        if ( bExist[remiander] )        {            return false;        }        bExist[remiander] = true;        b /= 10;    }    while ( c != 0 )    {        int remiander = c % 10;        if ( bExist[remiander] )        {            return false;        }        bExist[remiander] = true;        c /= 10;    }    return true;}int main(){    vector<int> nPandigitals;    for ( int a = 1; a < 10; ++a )    {        for ( int b = 1000; b < 10000; ++b )        {            if ( IsPandigital( a, b, a * b ) )            {                nPandigitals.push_back( a * b );            }        }    }    for ( int a = 10; a < 100; ++a )    {        for ( int b = 100; b < 1000; ++b )        {            if ( IsPandigital( a, b, a * b ) )            {                nPandigitals.push_back( a * b );            }        }    }    sort( nPandigitals.begin(), nPandigitals.end() );    int sum = nPandigitals[0];    for ( size_t i = 1; i < nPandigitals.size(); ++i )    {        if ( nPandigitals[i] != nPandigitals[i-1] )        {            sum += nPandigitals[i];        }    }    cout << sum << endl;    return 0;}