蛇形矩阵

题目:

现又一种蛇形矩阵,从第一行的行尾出发,顺时针移动,碰到边界或者自己的身体就转向,直至无法移动为止。请编写程序,得出答案。

输入样例:
8 6
输出样例:
20  21  22  23  24  1   
19  38  39  40  25  2   
18  37  48  41  26  3   
17  36  47  42  27  4   
16  35  46  43  28  5   
15  34  45  44  29  6   
14  33  32  31  30  7   
13  12  11  10  9   8   

代码示例:

#include <iostream>
#include <stdio.h>

using namespace std;
const int SIZE = 100;

int main()
{
    int n, m;                                   // n:蛇形方阵的行; m:蛇形方阵的列.
    cin >> m >> n;

    int enumerateM = m, enumerateN = n;         // enumerateM:备份m的值; enumerateN:备份n的值.
    int direction = 1, i = 0, j = n - 1;        // direction:用于判断方向; i:方阵数组的行; j:方阵数组的列.
    int number = 1;                             // number:储存在fArr里面的数值.

    int fArr[SIZE][SIZE];                       // fArr:用于储存蛇形方阵的数值数组.
    for (i = 0; i < SIZE; i++)                  // 数组初始化
        for (j = 0; j < SIZE; j++)
            fArr[i][j] = 0;

    if (m == 1)
    {
        fArr[0][n - 1] = 1;
    }
    else
    {
        int a = -1, b = -1;                         // 定义边界
        i = -1; j = n; m += 1;
    // 方向转向并赋值
        int frequency = 0;
        while (frequency < (enumerateM * enumerateN))
        {
            switch (direction % 4)
            {
            case 1:     // 方向向下,此时的特点是: 横坐标从0开始递增至m-1,纵坐标不变(为n-1)
                {
                    m = m - 1;
                    i += 1; j -= 1;
                    while (i <= m - 1)
                    {
                        fArr[i][j] = number;
                        number++;
                        i++; frequency++;
                    } direction++;
                } break;
            case 2:     // 方向向左,此时的特点是: 横坐标不变(m-1),纵坐标从n-1递减至0
                {
                    a += 1;
                    i -= 1; j -= 1;
                    while (j >= a)
                    {
                        fArr[i][j] = number;
                        number++;
                        j--; frequency++;
                    } direction++;
                } break;
            case 3:     // 方向向上,此时的特点是: 横坐标从m-1开始递减至0,纵坐标不变(为0)
                {
                    b += 1;
                    i -= 1; j += 1;
                    while (i >= b)
                    {
                        fArr[i][j] = number;
                        number++;
                        i--; frequency++;
                    } direction++;
                } break;
            case 0:     // 方向向右,此时的特点是: 横坐标不变(0),纵坐标从0递增至(m-1)-1
                {
                    n = n - 1;
                    i += 1; j += 1;
                    while (j <= n - 1)
                    {
                        fArr[i][j] = number;
                        number++;
                        j++; frequency++;
                    } direction++;
                } break;
            }
        }
    }
    // 输出数组
    for (i = 0; i < enumerateM; i++)
    {
        for (j = 0; j < enumerateN; j++)
            printf("%-4d", fArr[i][j]);
        cout << endl;
    }

    return 0;
}

转载于:https://www.cnblogs.com/JingWenxing/p/9945214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值