#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
// a*a + b*b = c*c
// then
// a = (x*x - y*y)* k
// b = (2*x*y) * k
// if a is greater than b, than swap a and b
// c = (x*x + y*y) * k
struct RightTriangle
{
int a;
int b;
int c; // hypotenuse
};
bool operator < ( const RightTriangle& left, const RightTriangle& right )
{
if ( left.a != right.a )
{
return left.a < right.a;
}
return left.b < right.b;
}
int main()
{
const int N = 1000;
set<RightTriangle> all;
for ( int x = 2; 2 * x * (x + 1) <= N; ++x )
{
for ( int y = 1; ; ++y )
{
int a = x * x - y * y;
int b = 2 * x * y;
int c = x * x + y * y;
if ( a > b )
{
swap( a, b );
}
int perimeter = a + b + c;
if ( perimeter > N )
{
break;
}
for ( int k = 1; k <= N / perimeter; ++k )
{
RightTriangle one = { a*k, b*k, c*k };
all.insert( one );
}
}
}
vector<int> solutions( N+1, 0 );
for ( set<RightTriangle>::iterator it = all.begin(); it != all.end(); ++it )
{
solutions[it->a + it->b + it->c]++;
}
cout << max_element( solutions.begin(), solutions.end() ) - solutions.begin() << endl;
return 0;
}
http://projecteuler.net/problem=39 [Answer:840]
最新推荐文章于 2011-12-10 11:42:42 发布