// Problem 39
// 14 March 2003
//
// If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
//
// {20,48,52}, {24,45,51}, {30,40,50}
//
// For which value of p <= 1000, is the number of solutions maximised?
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;
//************************************
// Method: IsRightAngleTriangle
// FullName: IsRightAngleTriangle
// Describe: 判断三角形是否为直角三角形
// Access: public
// Returns: bool
// Qualifier:
// Parameter: int leg1 直角边1
// Parameter: int leg2 直角边2
// Parameter: int leg3 斜边
//************************************
inline bool IsRightAngleTriangle(int leg1, int leg2, int leg3)
{
return leg1 * leg1 + leg2 * leg2 == leg3 * leg3;
}
void F1()
{
cout << "void F1()" << endl;
LARGE_INTEGER timeStart, timeEnd, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&timeStart);
const int MAX_PERIMETER = 1000; //最大的边长和
int maxCount = 0; //所有周长中,能组成的最多的直角三角形个数
int maxCountPerimeter = 0; //能组成的最多的直角三角形个数的周长
int count = 0; //当前的周长能组成的直角三角形个数
//边长和,取值范围[3,MAX_PERIMETER]
for(int perimeter = 3; perimeter <= MAX_PERIMETER; ++perimeter)
{
//初始化
count = 0;
//第三边,取值范围[1,perimeter/2)
for(int leg3 = perimeter / 2; leg3 > 0; --leg3)
{
//直角边1,取值范围[1,leg3)
for(int leg1 = 1; leg1 < leg3; ++leg1)
{
//直角边2
int leg2 = perimeter - leg1 - leg3;
//如果是直角三角形
if(IsRightAngleTriangle(leg1, leg2, leg3))
{
count++;
}
}
}
//记录最大值
if(count > maxCount)
{
maxCount = count;
maxCountPerimeter = perimeter;
}
}
cout << "能组成最多直角三角形个数的周长为" << maxCountPerimeter << ",一共" << maxCount << "个" << endl;
QueryPerformanceCounter(&timeEnd);
cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;
time_t currentTime = time(NULL);
char timeStr[30];
ctime_s(timeStr, 30, ¤tTime);
cout << endl << "By GodMoon" << endl << timeStr;
}
//主函数
int main()
{
F1();
return 0;
}
/*
void F1()
能组成最多直角三角形个数的周长为840,一共16个
Total Milliseconds is 2082.55
By GodMoon
Wed Apr 11 11:20:42 2012
*/
【ProjectEuler】ProjectEuler_039
最新推荐文章于 2024-08-04 14:55:04 发布