POJ1543 + 1013 解题报告

考察点:枚举法

 

    传送门:http://poj.org/problem?id=1543

 

POJ1543 完美立方

    题目描述:

 1 Description
 2 
 3 For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believed to be correct, though it is still undergoing scrutiny.) It is possible, however, to find integers greater than 1 that satisfy the "perfect cube" equation a^3 = b^3 + c^3 + d^3 (e.g. a quick calculation will show that the equation 12^3 = 6^3 + 8^3 + 10^3 is indeed true). This problem requires that you write a program to find all sets of numbers {a,b,c,d} which satisfy this equation for a <= N.
 4 Input
 5 
 6 One integer N (N <= 100).
 7 Output
 8 
 9 The output should be listed as shown below, one perfect cube per line, in non-decreasing order 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-decreasing order on the line itself. There do exist several values of a which can be produced from multiple distinct sets of b, c, and d triples. In these cases, the triples with the smaller b values should be listed first.
10 Sample Input
11 
12 24
13 Sample Output
14 
15 Cube = 6, Triple = (3,4,5)
16 Cube = 12, Triple = (6,8,10)
17 Cube = 18, Triple = (2,12,16)
18 Cube = 18, Triple = (9,12,15)
19 Cube = 19, Triple = (3,10,18)
20 Cube = 20, Triple = (7,14,17)
21 Cube = 24, Triple = (12,16,20)
Question

   思想:只需要把4个参数中的所有值都扫描一遍,看是否符合要求。

Source Code:

 1 //寻找完美立方等式  
 2 #include <iostream>  
 3 using namespace std;  
 4   
 5 int main()  
 6 {  
 7     int n;  
 8     cin >> n;  
 9     int a, b, c, d;  
10     int three[101];  
11     for( int i = 0; i < 101; i++ )  
12         three[ i ] = i * i *i;  
13     int result;  
14     for( a = 3; a <= n; a++)  
15         for( b = 2; b <=n; b++ )  
16             for( c = 2; c <= n; c++ )  
17                 for( d =2; d <= n; d++ )  
18                 {  
19                     result = ( three[ b ] + three[ c ] + three[ d ] ) ;  
20                     if( three[ a ] == result )  
21                         cout << "Cube = " << a << ",Triple = (" << b << "," << c << "," << d <<")" << endl;  
22                 }  
23 }  

结果:

题目要求输出的时候,b,c,d按照非降序排列输出,则b < c < d,修改代码,提交,AC通过。代码如下:

 1 //寻找完美立方等式  
 2 #include <iostream>  
 3 using namespace std;  
 4   
 5 int main()  
 6 {  
 7     int n;  
 8     cin >> n;  
 9     int a, b, c, d;  
10     int three[101];  
11     for( int i = 0; i < 101; i++ )  
12         three[ i ] = i * i *i;  
13     int result;  
14     for( a = 3; a <= n; a++)  
15         for( b = 2; b <=n; b++ )  
16             for( c = b; c <= n; c++ )  
17                 for( d =c; d <= n; d++ )  
18                 {  
19                     result = ( three[ b ] + three[ c ] + three[ d ] ) ;  
20                     if( three[ a ] == result )  
21                         cout << "Cube = " << a << ", Triple = (" << b << "," << c << "," << d <<")" << endl;  
22                 }  
23 }  
Source Code

当然,这样搜索的范围仍然太多,时间复杂度还是太大,我们需要做优化。

 

可以根据已知的东西来推出几个能够缩小范围的条件:

1.a一定大于b,c,d,所以bcd的for循环。只需要小于a就可以了,

2.在4层循环的第二层设立条件,如果 

1 three[a] < three[b] * 3;  

则没有必要继续向下循环了,因为c,和d向下循环的话只会越来越大。继续修改代码"

 1 //寻找完美立方等式  
 2 #include <iostream>  
 3 using namespace std;  
 4   
 5 int main()  
 6 {  
 7     int n;  
 8     cin >> n;  
 9     int a, b, c, d;  
10     int three[101];  
11     for( int i = 0; i < 101; i++ )  
12         three[ i ] = i * i *i;  
13     int result;  
14   
15     for( a = 3; a <= n; a++)  
16     {  
17         for( b = 2; b <a; b++ )  
18         {  
19             if( three[ a ] < three[ b ] * 3)  
20                 break;  
21             for( c = b; c < a; c++ )  
22                 for( d =c; d < a; d++ )  
23                 {  
24                     result = ( three[ b ] + three[ c ] + three[ d ] ) ;  
25                     if( three[ a ] == result )  
26                         cout << "Cube = " << a << ", Triple = (" << b << "," << c << "," << d <<")" << endl;  
27                 }  
28         }  
29     }  
30 }  

 

 

posted @ 2013-11-08 09:55  StruggleAndPersist  阅读(274)  评论(0)    收藏  举报