码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler: Problem 9 Special Pythagorean triplet

时间:2015-05-31 09:37:13      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:c++   project euler   

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;
}




Project Euler: Problem 9 Special Pythagorean triplet

标签:c++   project euler   

原文地址:http://blog.csdn.net/youb11/article/details/46276103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!