首页 > 其他 > 详细

Project Euler: Problem 9 Special Pythagorean triplet

时间:2015-05-31 09:34:32      阅读:238      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!