花了半小时按思路写的,发现连样例都不对,崩溃了
查看原帖
花了半小时按思路写的,发现连样例都不对,崩溃了
1351893
MuYuMC楼主2024/6/2 22:22
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
	int A[40][40] = {0},i,j,n,x,y;
	cin >> n;
	A[i][n / 2 + 1] = 1;
	x = 1;y = n / 2 + 1;
	for(i = 2;i < n * n;i++)
	{
		if(x == 1 && y != n)
		{
			A[n][y + 1] = i;
			x = n;
			y++;
		}
		else
		{
			if(y == n && x != 1)
			{
				A[x - 1][1] = i;
				x--;
				y = 1;
			}
			else
			{
				if(x == 1 && y == n)
				{
					A[2][n] = i;
					x = 2;
					y = n;
				}
				else
				{
					if(x != 1 && y != n)
					{
						if(A[x - 1][y + 1] == 0)
						{
							A[x - 1][y + 1] = i;
							x--;
							y++;
						}
					}
					else
					{
						A[x + 1][y] = i;
						x++;
					}
					continue;
				}
			}
		}
	}
	for(i = 1;i <= n;i++)
	{
		for(j = 1;j <= n;j++)
		{
			cout << A[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
} 
2024/6/2 22:22
加载中...