您的位置:首页 > Web前端

POJ1543 Perfect Cubes【暴力】

2017-12-27 17:17 399 查看
Perfect Cubes

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15909 Accepted: 8220
Description
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.
Input
One integer N (N <= 100).
Output
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.
Sample Input
24

Sample Output
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)

Source
Mid-Central USA 1995

问题链接POJ1543 Perfect Cubes


问题简述:(略)

问题分析

  这个问题适合暴力枚举,更加巧妙的方法也难以想出来。

  即便是枚举,也需要考虑能省则省。

程序说明
  使用立方数组,可以减少重复的立方计算。

  使用变量sum,可以减少重复的求和计算。

  当和sum大于a^3时,就不需要再试探下去了。

  b、c和d的值根据题意,1<b<=c<=d<=n,所以2<=b<=c<=d,得24<=b^3+c^3+d^3,取3<=a开始计算。

题记:(略)

参考链接:(略)

AC的C语言程序如下:

/* POJ1543 Perfect Cubes */

#include <stdio.h>

#define N 100
long cube[N + 1];

void setcube(int n)
{
int i;

for(i=0; i<=n; i++)
cube[i] = i * i * i;
}

int main(void)
{
setcube(N);

long n, a, b, c, d, sum;

while(~scanf("%ld", &n)) {
for(a=3; a<=n; a++)
for(b=2; b<=n; b++)
for(c=b; c<=n; c++)
for(d=c; d<=n; d++) {
sum = cube[b] + cube[c] + cube[d];
if(cube[a] == sum)
printf("Cube = %ld, Triple = (%ld,%ld,%ld)\n", a, b, c, d);
else if(sum > cube[a])
break;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: