打印螺旋矩阵

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

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

#include <iostream>
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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值