Perfect cube (enum) - learning algorithm

problem

Shaped like a ^ 3 = b ^ 3 + c ^ 3 + d ^ Equation 3 it is referred to as perfect cubic equation.
E.g. 12 ^ 3 ^ 3 = 6 + 8 + 10 ^ 3 ^ 3.
Write a program, for any given positive integer N (N≤100), looking for all
four-tuple (a, b, c, d), such that a3 = b3 + c3 + d3,
where a, b, c, d is greater than 1, or less N, and b <= c <= d.
Enter a positive integer N (N≤100).
The output of each line of output a perfect cube.
Output format: Cube = a, Triple = ( b, c, d)
wherein a, b, c, d, respectively, the actual location calculated values into quads with.
Follow the value of a, small to large output. When the same two values of a perfect cubic equation, then the
b-value output low priority remains the same, the smaller value of priority output c, d in the same small value to output

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)

Outline of Solution:
quartet enumeration cycle ABCD, the outermost layer a, d in the innermost layer, each layer is ascending enumeration of
a enumeration range [2, N]
B range [2, a-1 ]
C range [b, a-1]
d in the range [c, a-1]

#include<iostream>
 #include<cstdio>
 using namespace std;
 int main(){ 
 int N;
 cin>>N;
 for(int a=2;a<=N;a++)
  for(int b=2;b<a;b++)
   for(int c=b;c<a;c++)
    for(int d=c;d<a;d++)
     if(a*a*a==b*b*b+c*c*c+d*d*d)
     printf("Cube==%d,Triple=(%d,%d,%d)\n",a,b,c,d);
    return 0;
 }
Published 72 original articles · won praise 133 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_45822638/article/details/104991618