AC代码纯模拟

root  •  2年前


include

include

include

int a[21][21]; using namespace std; int main() {

int n,s,num=1,m,g,y,d;
cin>>n;
g=n-1;
memset(a,0,sizeof(a));
a[1][1]=1;
for(int k=2;k<=n;k++)
{
	if(k%2==0)
	 {
	 	s=k;
	 	m=k;
	  for(int j=1;j<=s;j++)
	 	{
	 		
	 		num++;
	 		a[j][m]=num;
	 		m--;
		 }
	 }
	 else
	 {
	 	s=k;
	 	m=1;
	 	for(int j=s;j>=1;j--)
	 	{
	 		
	 		num++;
	 		a[j][m]=num;
	 		m++;
		 }
	 }
	 }
	 num=n*n;
	 for(int k=1;k<=n-2;k++)
	 {
	 	if(k%2!=0)
	 	{
	 		y=n-k;
	 		for(int i=n;i>=g;i--)
	 		{
	 			num--;
	 			a[i][y]=num;
	 			y++;
			 }
	 		g=g-2;
		 }
		 else
		 {
		 	d=n;
		 	for(int j=n-k;j<=n;j++)
		 	{
		 		num--;
		 		a[j][d]=num;
		 		d--;
			 }
		 }
	 }
	 a[n][n]=n*n;
	 for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
        cout << setw(5) << a[i][j];

    cout << endl;
}
return 0;

}


评论:

请先登录,才能进行评论