ProjectEuler 9

求和为1000毕达哥斯加三元组,其实就是余弦定理的三个数。。。

 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 = 1000,可以设c>=b>=a,那么可以知a<1000/3,b<1000/2,然后令c=a-b,因此可以减少搜索量

代码如下:

private static int pyTriplet(int N) {
        for (int i = 1; i < N / 3; i++)
            for (int j = i + 1; j < N / 2; j++) {
                int k = N - i - j;
                if (i * i + j * j == k * k) {
                    System.out.println(i + "," + j + "," + k);
                    return i * j * k;
                }
            }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(pyTriplet(1000));
    }
原文地址:https://www.cnblogs.com/lake19901126/p/3072196.html