如果p是一个直角三角形的周长,三角形的三边长{a,b,c}都是整数。对于p = 120一共有三组解:
{20,48,52}, {24,45,51}, {30,40,50}
{20,48,52}, {24,45,51}, {30,40,50}
对于1000以下的p中,哪一个能够产生最多的解?
public class Problem39
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
System.out.print("answer: ");
howmany();
long end = System.currentTimeMillis();
System.out.print("time: ");
System.out.println(end - start);
}
static void howmany()
{
int max = 0;
int answer = 0;
for (int p = 4; p < 1000; p++)
{
int t = ab(p);
if (max < t)
{
answer = p;
max = t;
}
}
System.out.println(answer);
}
static int ab(int p)
{
int cishu = 0;
for (int a = 1; a < p; a++)
{
for (int b = 1; b < p; b++)
{
int c = p - a - b;
if (c < 0)
{
break;
}
if(a * a + b * b == c * c)
{
cishu++;
}
}
}
return cishu;
}
}
answer: 840
time: 250