题目描述
把数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;
int n,r,k,b[109][109];
int main()
{
cin>>n;
for(;;)
{
r++;
if(r>n/2)
{
if((n%2==1)&&(r==n/2+1))
{
k++;
b[r][r]=k;
}
break;
}
for(int j=r;j<=n-r;j++)
{
k++;
b[r][j]=k;
}
for(int j=r;j<=n-r;j++)
{
k++;
b[j][n-r+1]=k;
}
for(int j=n-r+1;j>=r+1;j--)
{
k++;
b[n-r+1][j]=k;
}
for(int j=n-r+1;j>=r+1;j--)
{
k++;
b[j][r]=k;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
代码有点长,题比较难。