您的位置:首页 > 编程语言 > Go语言

【Project Euler】【Problem 9】Special Pythagorean triplet

2013-03-27 18:50 453 查看


Special Pythagorean triplet


Problem 9

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.

Answer:
31875000


翻译:


特别的毕达哥拉斯三元数组


问题 9

一个毕达哥拉斯三元数组是有三个自然数组成, a

b

c,
满足 a2 + b2 = c2
例如, 32 + 42 = 9 + 16 = 25 = 52.
存在一个正确的毕达哥拉斯三元数组满足 a + b + c =
1000.

找到abc的乘积.

答案:
31875000
解法一:

#include <stdio.h>

int main(int argc, char *argv[])
{
int a=1;
int b=1;
int c=1;

int range = 1000;
for (a=1; a < range; a++)
{
for (b=1; b < range - a; b++)
{
c = range-a-b;
if (a*a+b*b==c*c)
{
printf("result is %d \n",a*b*c);
return 0;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: