简单的实现螺旋矩阵

16 15 14 13 

 5   4    3  12

 6   1    2  11

 7   8    9  10

const std::vector<std::vector<int>>/*&*/ CAlgorithm::screwMatrix(const int& width, const int&height )
{
	int differ = width - height;
	if (!(differ == 0 || differ == 1) || width + height < 2)
	{
		std::vector<std::vector<int>> buff;
		return buff;
	}


	std::vector<int> buff1(width,0);
	/*static*/ std::vector<std::vector<int>> buff0(height,buff1);

	int num = 1, start = width / 2 - 1, max = width * height;
	
	int j, count, x, y;

	for (int i = 2; i <= width + 1; i += 2)
	{
		j = 1;count = 1;
		//第一次取最里面的2X2的矩阵左上角点为初始点,之后每次把初始点斜向上移一格
		if (width <= 2)
		{
			if (height == 1)
			{
				x = -1;
				y = 0;
			}
			else 
			{
				x = y = 0;
			}
		}
		else
		{
			if (2 == i)
			{
				if (width % 2 == 0)
				{
					if (height % 2 == 0)
					{
						x = start;
						y = x;
					}
					else
					{
						x = start - 1;
						y = start;
					}
				}
				else
				{
					x = start;
					y = start +1;
				}
			}
			else
			{
				x -= 1;
				y -= 1;
			}
		}
		

		while (j < i)
		{
			switch(count)
			{
			case 1: ++x;break;
			case 2: ++y;break;
			case 3: --x;break;
			case 4: --y;break;
			}
			buff0[x][y] = num++;
			if (num > max)
			{
				break;
			}
			j++;
			if (j >= i && count < 4)
			{
				count++;
				j = 1;
			}
		}
	}

	for (int i = 0; i < height; i++)
		for(int j = 0; j < width; j++)
	{
		int temp = buff0[i][j];
		if(temp < 10)
			std::cout<<" "<<temp<<" ";
		else
			std::cout<<temp<<" ";
		if(j == width - 1)
			std::cout<<std::endl;
	}
	
	return buff0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现螺旋矩阵,你可以使用C语言编写一个函数,该函数将一个二维数组填充为螺旋矩阵。下面是一个示例代码: ```c #include <stdio.h> void generate_spiral_matrix(int n) { int matrix[n][n]; int value = 1; int row_start = 0; int row_end = n - 1; int col_start = 0; int col_end = n - 1; while (row_start <= row_end && col_start <= col_end) { // 上方行 for (int col = col_start; col <= col_end; col++) { matrix[row_start][col] = value++; } row_start++; // 右侧列 for (int row = row_start; row <= row_end; row++) { matrix[row][col_end] = value++; } col_end--; // 下方行 if (row_start <= row_end) { for (int col = col_end; col >= col_start; col--) { matrix[row_end][col] = value++; } row_end--; } // 左侧列 if (col_start <= col_end) { for (int row = row_end; row >= row_start; row--) { matrix[row][col_start] = value++; } col_start++; } } // 打印螺旋矩阵 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%3d ", matrix[i][j]); } printf("\n"); } } int main() { int n = 5; // 设置矩阵的大小 generate_spiral_matrix(n); return 0; } ``` 这段代码使用`generate_spiral_matrix`函数来生成一个螺旋矩阵。该函数接受一个整数 `n`,代表矩阵的大小。它使用一个二维数组 `matrix` 来存储螺旋矩阵的元素。然后,使用四个变量 `row_start`、`row_end`、`col_start` 和 `col_end` 来追踪当前螺旋的边界。 在每个螺旋的循环中,按照顺时针方向填充矩阵的元素。最后,使用两个嵌套循环打印生成的螺旋矩阵。 在 `main` 函数中,你可以设置 `n` 的值来确定螺旋矩阵的大小。运行程序后,将会打印出一个螺旋矩阵。 请注意,这只是一个简单的示例代码,并没有处理其他边界情况和错误检查。在实际应用中,你可能需要更全面的处理逻辑和错误处理机制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值