我非常不能理解错哪了
查看原帖
我非常不能理解错哪了
1521406
fyt_dy楼主2024/11/1 13:11
#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int n,x,y;
	cin >> n;
	x=0,y=0;
	int** arr = new int* [n];
	for (int i = 0; i < n; i++)
	{
		arr[i] = new int[n];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			arr[i][j] = 0;
		}
	}
	int mark = 1;
	arr[0][0] = mark;

	while (mark < n * n )
	{
		//向右走
		while (y <= n - 1 && arr[x][y + 1] == 0)
		{
			y++;
			mark++;
			arr[x][y] = mark;
		}
		//向左走
		while (y >= 1 &&arr[x][y - 1] == 0)
		{
			y--;
			mark++;
			arr[x][y] = mark;
		}
		//向上走
		while (x >= 1 &&arr[x - 1][y] == 0)
		{
			x--;
			mark++;
			arr[x][y] = mark;
			
		}
		//向下走
		while (x < n - 1 && arr[x + 1][y] == 0)
		{
			x++;
			mark++;
			arr[x][y] = mark;
		
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			printf("%3d", arr[i][j]);
		}
		cout << endl;
	}
	system("pause");
	return 0;
}
                                     
2024/11/1 13:11
加载中...