Special Pythagorean triplet
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).
特殊毕达哥拉斯三元组
毕达哥拉斯三元组是三个自然数 (a < b < c) 组成的集合,并满足
(a^2 + b^2 = c^2)
例如,(3^2 + 4^2 = 9 + 16 = 25 = 5^2)。
有且只有一个毕达哥拉斯三元组满足 (a + b + c = 1000)。求这个三元组的乘积 (abc)。
解题思路
没有想到比较好的办法,尝试枚举找出答案。
实现代码如下:
#include <bits/stdc++.h>
using namespace std;
int main() {
for (int i = 1; i <= 1000/3; i ++) {
for (int j = i; j < 1000-i-j; j ++) {
int k = 1000 - i - j;
if (i*i + j*j == k*k) {
// cout << i << ", " << j << ", " << k << endl;
cout << i * j * k << endl;
}
}
}
return 0;
}
运行后发现这对三元组为 (200, 375, 425),答案为 (31875000)。