⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlist.h

📁 功能:四阶幻方全解 运算时间:3、40秒 结果:7040个 环境:Visual Studio.net 2003的vc++的控制台项目
💻 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 + -