这个号开了也有半年了,感谢大家的支持,我以后会继续加油更新的!!!
接下来,我们来水一把。
oj:1163 - 打印蛇形矩阵(不是洛谷的)
输入一个n,输出一个蛇形矩阵,具体见样例
输入一个整数n,0<n<=10
输出对应的 n*n的矩阵
样例
输入
3
输出
1 2 3 8 9 4 7 6 5
代码如下
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N];
int main()
{
int n;
cin >> n;//n行m列
int left = 0, right = n - 1;
int top = 0, bottom = n - 1;
int k = 1;
while(left <= right || top <= bottom)
{
for(int i = left; i <= right && top <= bottom; i++)//构造最上面一行
{
a[top][i] = k++;
}
top++;
for(int i = top; i <= bottom && left <= right; i++)//构造最右侧一列
{
a[i][right] = k++;
}
right--;
for(int i = right; i >= left && top <= bottom; i--)//构造最下面一行
{
a[bottom][i] = k++;
}
bottom--;
for(int i = bottom; i >= top && left <= right; i--)//构造最左侧一列
{
a[i][left] = k++;
}
left++;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++) cout<< a[i][j] << " ";
cout << endl;
}
return 0;
}
作者牛不牛,拜拜啦