A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For 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.
- #include <stdio.h>
-
-
#define N 1000
-
int main(int argc, const char *argv[])
-
{
-
int a, b;
-
-
for (a=1; a<N/3; a++) {
-
for (b=N/3; b<(N-a);b++) {
-
if ((a*a + b*b) == (N-a-b)*(N-a-b))
-
goto end;
-
}
-
}
-
-
end:
-
printf("a:%d, b:%d, c:%d product:%d\n", a, b, (N-a-b), a*b*(N-a-b));
-
-
return 0;
-
}
-
-
利用交换律可以减少一些循环的次数。
阅读(1253) | 评论(0) | 转发(0) |