原题如下:
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.
翻译:
一个毕达哥拉斯三元组是一个包含三个自然数的集合,a<b<c,满足条件:
a2 + b2 = c2
例如:32 + 42 = 9 + 16 = 25 = 52.
已知存在并且只存在一个毕达哥拉斯三元组满足条件a + b + c = 1000。
找出该三元组中abc的乘积。
解题思路:这道题一看考的就是勾股定理啊,已知一个直角三角形,周长1000,求边长的积,呵呵,好怀念高中的时光呵。如果用数学方法解比较麻烦,用计算机语言的话其实还是挺简单的,定义a , b , c=1000-a-b 做一个检测是否满足勾股定理的if语句就ok了,代码如下:
public class Launcher {
public static void main(String[] args) {
int a=0;
int b=0;
int c=0;
for( a=1;a<500;a++){
for( b=a+1;b<500;b++){
c=1000-a-b;
if(Math.pow(a, 2)+Math.pow(b, 2)==Math.pow(c, 2)){
System.out.println(a*b*c);
System.exit(0);
}
}
}
}
}