2.样例输出:在 n×nn×n 方陈里填入 1,2,⋯,n×n1,2,⋯,n×n,要求填成蛇形。例如 n=4n=4 时方陈为:
10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4输入格式直接输入方陈的维数,即 nn 的值。(n≤100n≤100)
输出格式输出结果是蛇形方阵。
Sample Input
3Sample Output
7 8 1 6 9 2 5 4 3
3.代码如下:当然这个题就是简单的模拟题,关键是怎样模拟,刚拿到这道题时也是踌躇了好一会,还是决定写一下,因为他们也就只有四个方向遍历,主要是条件的控制,具体操作如下:
#include#include #include using namespace std; const int maxn=5e2+10; int a[maxn][maxn]; int main() { int t=1; ios::sync_with_stdio(false); int n; cin>>n; int x=0,y=n-1; a[x][y]=t; while(t =0&&!a[x][y-1]) a[x][--y]=++t; // 向左 while(x-1>=0&&!a[x-1][y]) a[--x][y]=++t; // 向上 while(y+1



