您的位置:首页 > 编程语言 > Go语言

Project Euler: Problem 9 Special Pythagorean triplet

2015-05-30 17:20 405 查看
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.

首先联立式子,消除c,得到关于ab的等式500000=1000a+1000b-ab
然后用a表示出b b=(500000-1000a)/(1000-a)
然后a从1开始取值,直到b为整数为止。

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int b;
	int i = 1;
	for (i = 1; i <= 998; i++)
	{
		if ((500000 - 1000 * i) % (1000 - i) == 0)
		{
			b = (500000 - 1000 * i) / (1000 - i);
			break;
		}
	}
	cout << i * b *( 1000 - i - b) << endl;
	system("pause");
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: