Project Euler__problem 9

Problem 9


Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.Find the product abc.


特殊畢達哥拉斯三元組

畢達哥拉斯三元組是三個自然數a < b < c組成的集合,並滿足

a2 + b2 = c2

例如,32 + 42 = 9 + 16 = 25 = 52

有且只有一個畢達哥拉斯三元組滿足 a + b + c = 1000。求這個三元組的乘積abc。



暴力解....

#include<iostream>  


void main()
{
	int a,b,c;
	for (a = 1; a < 334; a++)
		for (c = 334; c < 1000; c++)
		{
			b = 1000 - a - c;
			if (a*a + b*b == c*c)
			{
				std::cout << "這個數組爲:" << std::endl;
				std::cout << "a= " << a << std::endl;
				std::cout << "b= " << b << std::endl;
				std::cout << "c= " << 1000 - a - b << std::endl;
				std::cout << "abc=" << a*b*(1000 - a - b) << std::endl;
				break;
			}
		}

	system("pause");
}

最後的答案:31875000

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