Perfect Cubes
Problem Description
For hundredsof years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 suchthat a^n = b^n + c^n, has remained elusivelyunproven. (A recent proof is believed to be correct, though it is stillundergoing scrutiny.) It is possible, however, to find integers greater than 1that satisfy the ``perfect cube'' equation a^3 = b^3 + c^3 + d^3 (e.g. a quickcalculation will show that the equation 12^3 = 6^3 + 8^3 + 10^3 is indeedtrue). This problem requires that you write a program to find all sets of numbers {a, b, c, d} which satisfythis equation for a <= 200.
Output
The outputshould be listed as shown below, one perfect cube per line, in non-decreasingorder of a (i.e. the lines should be sorted by their a values). The
values of b, c, and d should also be listed in non-decreasingorder on the line itself. There do exist several values of a which can beproduced from multiple distinct sets of b, c, and d triples(三方的).
In these cases, thetriples with the smaller b values should belisted first.(由下划线部分可知,求的是a^3=b^3+c^3+d^3,且b最小)
The first part of the output is shown here:(由此句可知,a是从6开始的,所以程序中a最小为6即可)
Cube = 6, Triple = (3,4,5)
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)
Note: The programmer will need to be concerned with an efficientimplementation. The official time limit for this problem is 2 minutes, and itis indeed possible to write a solution to this problem which executes in under2 minutes
on a 33 MHz 80386 machine. Due to the distributed nature of thecontest in this region, judges have been instructed to make the official timelimit at their site the greater of 2 minutes or twice the time taken by thejudge's solution on the machine being used
to judge this problem
//*********************************** //* 程 序 名:Cube.cpp * //* 作 者:何香 * //* 编制时间:2013年9月12日 * //* 主要功能:计算a^3=b^3+c^3+d^3 * //*********************************** #include <iostream> using namespace std; int main() { int a,b,c,d; for (a=6;a<=200;++a) { for (b=2;b<=200;++b)//必须从b开始循环,因为b最小;b<c<d { for (c=b;c<=200;++c) { for (d=c;d<=200;++d) { if (a*a*a==b*b*b+c*c*c+d*d*d) { cout<<"Cube = "<<a<<","<<" Tripe = ("<<b<<","<<c<<","<<d<<")"<<endl;//注意格式 } } } } } return 0; }