顺时针打印矩阵

void PrintMatrixClockwise(int** arr, int columns, int rows)
{
	if (arr == nullptr || columns <= 0 || rows <= 0)
		return;

	int start = 0;
	while (columns > start * 2 && rows > start * 2)
	{
		PrintMatrixInCircle(arr,columns,rows,start);
		++start;
	}
	return;
}
void PrintMatrixInCircle(int** arr, int columns, int rows, int start)
{
	int endy = rows - start - 1;
	int endx = columns - start - 1;
	for (int i = start; i <= endx; ++i)
	{
		int number = arr[start][i];
		std::cout << number << std::endl;
	}
	if (start < endy)
	{
		for (int i = start; i <= endy; ++i)
		{
			int number = arr[i][endx];
			std::cout << number << std::endl;
		}
	}
	if (start < endx && start < endy)
	{
		for (int i = endx-1; i >= start; --i)
		{
			int number = arr[endy][i];
			std::cout << number << std::endl;
		}
	}
	if (start < endx && start < endy - 1)
	{
		for (int i = endy-1; i >= start+1; --i)
		{
			int number = arr[i][start];
			std::cout << number << std::endl;
		}
	}
}

  

posted on 2021-02-16 21:53  Noora&w  阅读(35)  评论(0编辑  收藏  举报