问题描述:
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?
解决问题:
public class Problem39 { public static int find_way(int number){ int count = 0; for(int c=500; c>0; c--){ for(int a = c-1; a>(number-c)/2&&a>0; a--){ int b = number - a - c; if(b>0&&b*b+a*a==c*c){ // System.out.println("a:"+a+",b:"+b+",c:"+c); count++; } } } return count; } public static void main(String[] args){ int max = 0; int mark = 0; for(int i=1000; i>0; i--){ int tmp = find_way(i); if(tmp>max){ max =tmp; mark = i; } } System.out.println(max+":"+mark); } }