Take the number of basic exercises

Resource limit
Time limit: 1.0s Memory limit: 512.0MB


Problem description
  round shape fetching is to fetch the number along the edge of the matrix. If there are countless fetches in the current direction or have been fetched, then turn left 90 degrees. It starts at the upper left corner of the matrix, and the direction is downward.


Input format    The
  first line of input is two positive integers m, n not exceeding 200, representing the rows and columns of the matrix. In the next m rows, n integers in each row represent this matrix.


Output format    There
  is only one line of output, a total of mn numbers, which are the results obtained by the input matrix shaping. Separate the numbers with a space, and do not have extra spaces at the end of the line.


Sample input
3 3
1 2 3
4 5 6
7 8 9
Sample output
1 4 7 8 9 6 3 2 5


Sample input
3 2
1 2
3 4
5 6
Sample output
1 3 5 6 4 2


The source code is as follows:

#include<iostream>
#include<string>
using namespace std;
int  a[200][200];
int i = 0, j = 0;
int i_1 = 0, j_1 = 0;
void getdate(int m, int n)//m*n的矩阵
{
    
    
	for (int x = 0; x<m; x++)
		for (int y = 0; y < n; y++)
		{
    
    
			cin >> a[x][y];
		}
}
void show(int m, int n)//结果输出
{
    
    
	
	int b[200][200];
	int sum = 0;
							
	for (int x = 0; x < m; x++)	//对数组b的初始化。0代表对应位置上的数已被读取,1反之。	
		for (int y = 0; y < n; y++)//也可以用memset()来进行初始化,在string的头文件中。
			b[x][y] = 1;
	
	while (sum < m*n)
	{
    
    
		for (i; i < m - i_1; i++)//向下走
		{
    
    
			if (b[i][j] == 1 && b[i][j] > 0)
			{
    
    
				cout << a[i][j] << " ";
				b[i][j] = 0;
				sum++;
			}
		}

		for (j; j < n - j_1; j++)//向右走
		{
    
    
			if (b[i - 1][j] == 1 && b[i - 1][j] > 0)
			{
    
    
				cout << a[i - 1][j] << " ";
				b[i - 1][j] = 0;
				sum++;
			}
		}

		for (i = m - 2 - i_1; i >= 0+i_1; i--)//向上走
		{
    
    
			if (b[i][j - 1] == 1 && b[i][j - 1] > 0)
			{
    
    
				cout << a[i][j - 1] << " ";
				b[i][j - 1] = 0;
				sum++;
			}
		}

		for (j = n - 2 - j_1; j > 0; j--)//向左走
		{
    
    
			if (b[i + 1][j] == 1 && b[i + 1][j] > 0)
			{
    
    
				cout << a[i + 1][j] << " ";
				b[i + 1][j] = 0;
				sum++;
			}
		}
		i_1++; j_1++;
		i = i_1; j = j_1;
	}
	cout << endl;
}
int main()
{
    
    
	int m, n;
	cout << "请输入两个正整数,分别为行数,列数:" << endl;
	cin >> m >> n;
	getdate(m, n);
	show(m, n);
}

Evaluation data: (you can copy and verify by yourself)

  • 7 5
    2995 1945 4827 5436 2400
    4607 3902 153 292 2385
    7424 8719 9721 9898 5447
    1732 4774 1541 1869 9915
    5673 6305 7038 9894 8709
    3817 1331 342 7676 4664
    5144 7711 8259 6868 5553

    2995 4607 7424 1732 5673 3817 5144 7711 8259 6868 5553 4664 8709 9915 5447 2385 2400 5436 4827 1945 3902 8719 4774 6305 1331 342 7676 9894 1869 9898 292 153 9721 1541 7038


Evaluation results:

Insert picture description here


Thank you for your advice.

Guess you like

Origin blog.csdn.net/weixin_49243150/article/details/112935817