Fortune left France title 5 basis class3- circling printing matrix

Fortune left France title 5 basis class3- circling printing matrix

1. Topic

Given an integer matrix matrix, please print it in accordance with the circular motion of the way.
For example: 1,234,567,891,011,121,314 15 16 Printing results: 1,2,3,4,8,12,16,15,14,13,9,5,6,7, 11, 10
[] required extra space complexity is O (1).

Here Insert Picture Description

2. Analysis

(1) The title consider a, b, c, d four variables to control the border, as shown below a, c indicates a row, b, d represents the column (a, b) is the upper left corner, (c, d) is the lower right corner. Accordance with the subject requires printing a transverse, d wales, c reverse rampant, b reverse wale. After a ++, b ++, c-, d-, that is, the upper-left corner to the lower right corner point moves to the upper left corner to the lower right corner point move. Continue printing inside the square frame, and then change a, b, c, d until the value (a, b), (c, d) coincident with the wrong or end.

Here Insert Picture Description
(2) In order to facilitate printing, in consideration FIG printing mode, the inflection point is not set for the start of the next printing, the number of remaining prints.

Here Insert Picture Description

3. core code

(2) a group of the final print to form individual rows and columns as in FIG title, and does not constitute a square frame for printing, it needs to be determined, with the proviso that ac and bd。

void print_num(int arr[][height],int a,int b,int c,int d)
{
	if(a==c)
	{
		for(int i = b;i <= d;i++)
		{
			cout<<arr[a][i]<<" ";
		}
	}
	else if(b==d)
	{
		for(int i = a;i <= c;i++)
		{
			cout<<arr[i][b]<<" ";
		}
	}
	else
	{
		int cur_i = a;
		int cur_j = b;
		while(cur_j != d)
		{
			cout<<arr[a][cur_j]<<" ";
			cur_j++;
		}
		while(cur_i != c)
		{
			cout<<arr[cur_i][d]<<" ";
			cur_i++;
		}
		while(cur_j != b)
		{
			cout<<arr[c][cur_j]<<" ";
			cur_j--;
		}
		while(cur_i != a)
		{
			cout<<arr[cur_i][b]<<" ";
			cur_i--;
		}
	}
}

(2) calls the print condition is the upper left corner point can not be with the wrong point lower right corner, with equal numbers refer to when they coincide when the last remaining number must be printed

while(a <= c && b <= d)
	{
		print_num(arr,a,b,c,d);
		a++,b++,c--,d--;
	}

4. The complete code

#include<iostream>
#define height 3
#define width 3
using namespace std;

void print_num(int arr[][height],int a,int b,int c,int d)
{
	if(a==c)
	{
		for(int i = b;i <= d;i++)
		{
			cout<<arr[a][i]<<" ";
		}
	}
	else if(b==d)
	{
		for(int i = a;i <= c;i++)
		{
			cout<<arr[i][b]<<" ";
		}
	}
	else
	{
		int cur_i = a;
		int cur_j = b;
		while(cur_j != d)
		{
			cout<<arr[a][cur_j]<<" ";
			cur_j++;
		}
		while(cur_i != c)
		{
			cout<<arr[cur_i][d]<<" ";
			cur_i++;
		}
		while(cur_j != b)
		{
			cout<<arr[c][cur_j]<<" ";
			cur_j--;
		}
		while(cur_i != a)
		{
			cout<<arr[cur_i][b]<<" ";
			cur_i--;
		}
	}
}
int main()
{
	int arr[height][width] = {1,2,3,4,5,6,7,8,9};
	//int arr[height][width] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
	int a = 0,b = 0,c = height - 1,d = width - 1;
	while(a <= c && b <= d)
	{
		print_num(arr,a,b,c,d);
		a++,b++,c--,d--;
	}
	system("pause");
	return 0;
}

5. The output

Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1388

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103656273