Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239181
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-04 00:00:23

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.

  1. #include <stdio.h>

  2. #define N 1000
  3. int main(int argc, const char *argv[])
  4. {
  5.     int a, b;

  6.     for (a=1; a<N/3; a++) {
  7.         for (b=N/3; b<(N-a);b++) {
  8.             if ((a*a + b*b) == (N-a-b)*(N-a-b))
  9.                 goto end;
  10.         }
  11.     }

  12. end:
  13.     printf("a:%d, b:%d, c:%d product:%d\n", a, b, (N-a-b), a*b*(N-a-b));

  14.     return 0;
  15. }

  16. 利用交换律可以减少一些循环的次数。


阅读(1203) | 评论(0) | 转发(0) |
0

上一篇:euler8

下一篇:euler10

给主人留下些什么吧!~~