📄 sqlist.h
字号:
//#include <iostream>
const int DIM = 5;
const int INITSIZE = 100;//顺序表的初始化大小
const int SUM = (1 + DIM * DIM) * DIM / 2;//每行的和(如四维时和为应34)
//using namespace std;
/////////////////////////////////////////////SqList类定义开始**********************************************
class SqList
{
protected:
int max;//当前最大空间
int length;//当前元素个数
public:
int (*list)[DIM];
SqList()
{
length = 0;
list = new int[INITSIZE][DIM];
max = INITSIZE;
}
int GetLength()
{
return length;
}
~SqList()
{
delete []list;
list = NULL;
}
bool AppendLine(int p[DIM]);//在顺序表末尾添加一行。
void CountAll();//把所有满足条件的行求出来。
void Display();//显示所有满足条件的行,及总数(length)。
};
/////////////////////////////
bool SqList::AppendLine(int p[DIM])
{
if (length >= max)
{
if(!(list = (int (*)[DIM])realloc(list, (max+INCREASMENT)*DIM*sizeof(int)) ))
{
exit(0);
}
max += INCREASMENT;
}
for (int i = 0; i < DIM; i++)
{
list[length][i] = p[i];
}
length++;
return true;
}
///////////////////////////
void SqList::CountAll()//求出所有的行,1~DIM中和为SUM的DIM个不同数为一行。
{
int i = 1;
int e = 1;
int arraytemp[DIM];
Stack <int> s;
s.Push(e);
while(!s.StackEmpty() || e <= DIM*DIM)
{
if (i == DIM)
{
int sum = 0;
for (int k = 0; k < DIM; k++)
{
s.GetOne(k, e);
sum += e;
arraytemp[k] = e;
}
if (sum == SUM)
{
AppendLine(arraytemp);
}
s.Pop(e);
i--;
}
while(i < DIM)
{
e++;
if (e > DIM*DIM)
{
break;
}
s.Push(e);
i++;
}
if (e > DIM*DIM)
{
s.Pop(e);
i--;
}
}//while
}
//////////////////////////////////////////
void SqList::Display()
{
int (*p)[DIM];
p = list;
for (int i = 0; i < length; i++)
{
for (int j = 0; j < DIM; j++)
{
cout << (*p)[j] << " " ;
}
cout << endl;
p++;
}
cout << "总计:" << length << endl;
}
//////////////////////////////////////////////SqList类定义结束*************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -