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; }
相关文章推荐
- http://projecteuler.net/problem=27 [Answer:-59231]
- http://projecteuler.net/problem=30 [Answer:443839]
- http://projecteuler.net/problem=38 [Answer:932718654]
- http://projecteuler.net/problem=28 [Answer:669171001]
- http://projecteuler.net/problem=34 [Answer:40730]
- http://projecteuler.net/problem=35 [Answer:55]
- http://projecteuler.net/problem=36 [Answer:872187]
- http://projecteuler.net/problem=14 [Answer:837799]
- http://projecteuler.net/problem=19 [Answer:171]
- http://projecteuler.net/problem=25 [Answer:4782]
- http://projecteuler.net/problem=29 [Answer:9183]
- http://projecteuler.net/problem=5 [Answer:232792560]
- http://projecteuler.net/problem=20 [Answer:648]
- http://projecteuler.net/problem=39 [Answer:840]
- http://projecteuler.net/problem=6 [Answer:25164150]
- http://projecteuler.net/problem=13 [Answer:5537376230]
- http://projecteuler.net/problem=15 [Answer:137846528820]
- http://projecteuler.net/problem=21 [Answer:31626]
- http://projecteuler.net/problem=7 [Answer:104743]
- http://projecteuler.net/problem=8 [Answer:40824]