Project Euler——Problem 9 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
Project Euler——Problem 8
Project Euler——Problem 10

Project Euler——Problem 9

daybreakcx posted @ 2009年7月17日 21:42 in Prject Euler , 971 阅读

原题与答案:

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^(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.

Answer:31875000

 

分析与解答:

这个题目是求一组互不相等的勾股数使得他们的和为1000,求他们的乘积。直接的想法就是直接枚举,当然排除掉不可能的情况后就很快可以得到结果了,下面是程序源代码:

 

  1. #include<stdio.h>
  2. int a,b,c;
  3. bool yet;
  4. int main()
  5. {
  6.         yet=false;
  7.         for (a=1;a<=332;a++)
  8.         {
  9.                 for (b=a+1;!yet&&a+b+b+1<=1000;b++)
  10.                         if (a*a+b*b==(1000-a-b)*(1000-a-b))
  11.                         {
  12.                                 yet=true;
  13.                                 break;
  14.                         }
  15.                 if (yet) break;
  16.         }
  17.         printf("%d\n",a*b*(1000-a-b));
  18.         return 0;
  19. }

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter