Project Euler——Problem 9 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
Project Euler——Problem 9
daybreakcx
posted @ 2009年7月17日 21:42
in Prject Euler
, 990 阅读
原题与答案:
Problem 9
25 January 2002
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a
2 + b 2 = c 2For example, 3
2 + 4 2 = 9 + 16 = 25 = 5 2 .There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
Answer:31875000
分析与解答:
这个题目是求一组互不相等的勾股数使得他们的和为1000,求他们的乘积。直接的想法就是直接枚举,当然排除掉不可能的情况后就很快可以得到结果了,下面是程序源代码:
-
#include<stdio.h>
-
int a,b,c;
-
bool yet;
-
int main()
-
{
-
yet=false;
-
for (a=1;a<=332;a++)
-
{
-
for (b=a+1;!yet&&a+b+b+1<=1000;b++)
-
if (a*a+b*b==(1000-a-b)*(1000-a-b))
-
{
-
yet=true;
-
break;
-
}
-
if (yet) break;
-
}
-
return 0;
-
}