把数1,2,3,4,5,…,N*N按照“蛇形3”放入N*N矩阵的中,输出结果。
下面是N=6的蛇形3的图示
输入格式
第一行1个正整数:N,范围在[1,100]。
输出格式
N行,每行有N个整数。
输入/输出例子1
输入:
3
输出:
1 2 3
8 9 4
7 6 5
不废话,上代码,
#include<bits/stdc++.h>
using namespace std;
long long n,a[2000][2000],t=1,m;
int main(){
cin>>n;
if(n%2==1)
m=n/2+1;
else
m=n/2;
for(int i=1;i<=m;i++)
{
for(int j=i;j<=n-i+1;j++)
{
a[i][j]=t;
t++;
}
for(int j=1+i;j<=n-i+1;j++)
{
a[j][n-i+1]=t;
t++;
}
for(int j=n-i;j>=i;j--)
{
a[n-i+1][j]=t;
t++;
}
for(int j=n-i;j>=1+i;j--)
{
a[j][i]=t;
t++;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]<=9)
cout<<a[i][j]<<" ";
else if(a[i][j]>=10&&a[i][j]<=99)
cout<<a[i][j]<<" ";
else if(a[i][j]>=100&&a[i][j]<=999)
cout<<a[i][j]<<" ";
else if(a[i][j]>=1000&&a[i][j]<=9999)
cout<<a[i][j]<<" ";
else
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
/*点个赞再走,靓仔靓女。
50赞出方法2*/