您的位置:首页 > 理论基础 > 计算机网络

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

2011-12-03 14:36 435 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  include c