蛇形矩阵

蛇形矩阵

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;

const int N = 110;
int a[N][N];

int main()
{
    int h,l;
    cin >> h >> l;

    int left = 0, right = l - 1;
    int top = 0, bottom = h - 1;
    int sum = 1;
    while(left <= right || top <= bottom)
    {
        for(int i = left; i <= right && top <= bottom; i++)
            a[top][i] = sum++;//处理第一行
        top++;//行数下移
        for(int i = top; i <= bottom && left <= right; i++)
            a[i][right] = sum++;//处理最右列
        right--;//最右列左移一位
        for(int i = right; i >= left && top <= bottom; i--)
            a[bottom][i] = sum++;//处理最底部一行
        bottom--;//最低行上移
        for(int i = bottom; i >= top && left <= right; i--)
            a[i][left] = sum++;//处理最左一行
        left++;//最左行右移一位
    }
    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < l; j++) cout<< a[i][j] << " ";
        cout << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值