/*a^3 + b^3 = c^3 + d^3
其中:“^”表示乘方。a、b、c、d是互不相同的小于30的正整数。*/
#include<iostream.h>
typedef struct node
{
int data[4];
struct node *next;
}node,*nodep;
void sort(int *array)
{
int k;
int temp;
for(int i = 0;i < 4; ++i)
{
k = i;
for(int j = i + 1;j < 4;++j)
{
if(array[k] > array[j]) k = j;
}
temp = array[k];
array[k] = array[i];
array[i] = temp;
}
}
void main()
{
nodep head,p,q;
head=new (node);
head->next=NULL;
q=head;
int flag=0;
int a,b,c,d;
for(a=1;a<27;a++)
for(b=a;b<28;b++)
if(a!=b)
for(c=1;c<29;c++)
if(c!=a && c!=b)
for(d=c;d<30;d++)
if(d!=a && d!=b && d!=c && (a*a*a+b*b*b-c*c*c-d*d*d==0))
{
// cout<<a<<","<<b<<","<<c<<","<<d<<endl;
p=new(node);
p->data[0]=a;
p->data[1]=b;
p->data[2]=c;
p->data[3]=d;
sort(p->data);
nodep px=head;
while(px)
{
int m;
for(m=0;m<4;m++)
{
if(px->data[m]!=p->data[m])
break;
}
if(m==4)
{
flag=1;
break;
}
else
//否则指针后移
px=px->next;
}
if(flag)
{//结束标志为一时,即新结点中的数据与已存结点数据重复时,
//释放结点,并结束本次循环
delete(p);
continue;
}
//将新结点连接到链表中
p->next=NULL;
q->next=p;
q=p;
//输出找到的组合
for(int n=0;n<4;n++)
{
cout<<p->data[n]<<",";
}
cout<<endl;
}
}