Project Euler NO39

如果p是一個直角三角形的周長,三角形的三邊長{a,b,c}都是整數。對於p = 120一共有三組解:
{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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章