oj:1163 - 打印蛇形矩阵

这个号开了也有半年了,感谢大家的支持,我以后会继续加油更新的!!!

接下来,我们来水一把。

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;
}

作者牛不牛,拜拜啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值