标签:c++ project euler
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c =
1000.
Find the product abc.
首先联立式子,消除c,得到关于ab的等式500000=1000a+1000b-ab
然后用a表示出b b=(500000-1000a)/(1000-a)
然后a从1开始取值,直到b为整数为止。
#include <iostream> #include <string> using namespace std; int main() { int b; int i = 1; for (i = 1; i <= 998; i++) { if ((500000 - 1000 * i) % (1000 - i) == 0) { b = (500000 - 1000 * i) / (1000 - i); break; } } cout << i * b *( 1000 - i - b) << endl; system("pause"); return 0; }
Project Euler: Problem 9 Special Pythagorean triplet
标签:c++ project euler
原文地址:http://blog.csdn.net/youb11/article/details/46276103