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?
调试欢乐多
整数 P 为一直角三角形的周长,{a,b,c}为其三条整数边
如 p = 120 时
有{20,48,52}{24,45,51}{30,40,50}3种形式
当 P <= 1000时,拥有最多形式的是哪个
/*
a*a + b*b = c*c
a + b + c = 1000;
a + b > c
a>0, b>0, c>0
a <= b < c
==>
333 < c < 500
result 200, 375, 425
*/ class program
{ static void Main(string[] args)
{
List<TriAngleData> list = TriAngle.GetResult();
TriAngle.PrintTriangles(list);
}
} public class TriAngle
{
public static List<TriAngleData> GetResult()
{
List<TriAngleData> list = new List<TriAngleData>();
int a, b, c; for (c = 334; c < 500; c++)
{
for (b = (1000-c)/2; b < c; b++)
{
a = 1000 - b - c;
if (a * a + b * b == c * c)
{
list.Add(new TriAngleData() { A = a, B = b, C = c });
}
}
}
return list;
} public static void PrintTriangles(List<TriAngleData> list)
{
Console.WriteLine("Triangle Number is: " + list.Count);
foreach (TriAngleData tria in list)
{
Console.WriteLine(tria);
}
}
}
public class TriAngleData
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; } public override string ToString()
{
return "a=" + A + ", b=" + B + ", c=" + C;
}
}