打印螺旋矩阵

首先,看一下螺旋矩阵的样子.如下图:

求螺旋数组的代码如下(DEV-CPP平台):

#include 
using namespace std;

int** alloc_mat(int round); //动态二维数组的分配 
void del_mat(int **mat, int round); //删除动态分配的二维数组
void print_mat(int **mat, int round); //打印二维数组
void fill_mat(int **mat, int round); //填充数组为螺旋数组
 
int main()
{
	int **spiral_mat;	
	int round;
	do{
		cout << "请输入螺旋数组的层数:" << endl;
		cin >> round;
		system("cls");
	}while (round <= 0);
	
	spiral_mat = alloc_mat(round);
	fill_mat(spiral_mat,round);
	print_mat(spiral_mat, round);
	del_mat(spiral_mat, round);			
	system("pause");
	return 0;
}

int** alloc_mat(int round)
{
	int **mat;
	int i, j, dim;
	dim = (round * 2) - 1;
	mat = new int*[dim];
	for (i = 0; i < dim; i++)
	{
		*(mat+i) = new int[dim];
	}
	for (i = 0; i < dim; i++)
		for (j = 0; j < dim; j++)
			mat[i][j] = 0;
	return mat;
}
void del_mat(int **mat, int round)
{
	int i,dim;
	dim = (round * 2) - 1;
	for(i = 0; i < dim; i++)
		delete[] mat[i];
	delete[] mat;
}
	
void print_mat(int **mat, int round)
{
	int i, j, dim;
	dim = (round * 2) - 1;
	cout << "你输入的" << round << "层螺旋数组为:"  << endl << endl;
	for(i = 0; i < dim; i++)
	{
		for (j = 0; j < dim; j++)
			printf("%5d", mat[i][j]);
		cout << endl;
	}
	cout << endl;
}

void fill_mat(int **mat, int round)
{
	int centre_x, centre_y;
	int r = 0;
	int val = 1;
	
	centre_x = round - 1;
	centre_y = round - 1;
	
	mat[centre_x][centre_y] = val++;
	r++;
	
	int i;
	while (r < round)
	{
		i = centre_x - r + 1;
		while (i <= centre_x + r)//右上->右下 
			mat[i++][centre_y + r] = val++;
		
		i = centre_y + r - 1;
		while (i >= centre_y - r)//右下->左下 
			mat[centre_x + r][i--] = val++;
		
		i = centre_x + r - 1;
		while (i >= centre_x - r)//左下->左上 
			mat[i--][centre_y - r] = val++;
		
		i = centre_y - r + 1;
		while (i <= centre_y + r)//左上->右上 
			mat[centre_x - r][i++] = val++;		
		
		r++;
	}		
}

我们还可以进一步把fill_mat函数进行优化. 上面fill_mat函数是采取一层一层赋值,然后一层里再沿顺时针方向,取一圈为4条边, 即一条边一条边赋值.

我们可以直接一圈圈赋值.参考下面fill_mat2函数.

void fill_mat2(int **mat, int round)
{
	int centre_x, centre_y;
	int r = 0;
	int val = 1;
	
	centre_x = round - 1;
	centre_y = round - 1;
	
	mat[centre_x][centre_y] = val++;
	r++;
	
	int i;
	while (r < round)
	{
		for (i = 0; i < r * 2; i++)
		{
			mat[centre_x - r + 1 + i][centre_y + r] = val + i;
			mat[centre_x + r][centre_y + r - 1 - i] = val + 2 * r + i;
			mat[centre_x + r - 1 - i][centre_y - r] = val + 2 * r * 2 + i;
			mat[centre_x - r][centre_y - r + 1 + i] = val + 2 * r * 3 + i;
		}
		val += 4 * 2 * r;
		r++;
	}		
}

 -------------

如果矩阵是由外往内螺旋,如下图.


 那么如何实现呢? 其实也不难, 代码如下:

void fill_mat(int **mat, int mat_size)
{
    int i = 0, j = -1, r = mat_size; //i代表行, j代表列
    int val = 1;
    int count;

    while (r > 0)
    {
        for (count = 0; count < r; count++)
            mat[i][++j] = val++;

        for (count = 0; count < r - 1; count++)
            mat[++i][j] = val++;

        for (count = 0; count < r - 1; count++)
            mat[i][--j] = val++;

        for (count = 0; count < r - 2; count++)
            mat[--i][j] = val++;

        r -= 2;
    }
}

你可能感兴趣的:(笔试面试类,delete,system,优化,平台)