题目描述
下面这样的方阵很有规律,称为蛇形方阵。例如3*3的:
1 2 3
6 5 4
7 8 9
现在给定边长,输出相应的蛇形方阵。
输入格式
输入文件名:633.in
1个整数n,表示要输出n*n的蛇形方阵,1<=n <=100。
输出格式
输出文件名:633.out
n行,每行n个整数,空格隔开。
输入/输出例子1
输入:
4
输出:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
提示:输出数无需上面样例输出那样整齐。
程序
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++)
{
if(i%2==1)
{
for(int j=(i-1)*n+1;j<=i*n;j++)
cout<<j<<" ";cout<<endl;
}
else
{
for(int j=i*n;j>=(i-1)*n+1;j--)
cout<<j<<" ";cout<<endl;
}
}
return 0;
}