m0_59603572 2021-07-05 08:31 采纳率: 66.7%
浏览 120
已采纳

c语言 c++ 蛇形填数

蛇形填数,给出N*N的矩阵,要求用程序填入下列蛇形排列形式得数(例如N=5)则形成
1 3 4 10 11
2 5 9 12 19
6 8 13 18 20
7 14 17 21 24
15 16 22 23 25

  • 写回答

1条回答 默认 最新

  • 黑马星云 2021-07-05 08:56
    关注

    首先输入一个整数表示矩阵的边长,再输入一个数(1~3),2就是此题,望采纳
    !!

    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    /*
    本质上是一个按照规律构建二维矩阵的问题
    */
    
    void Inversion(int n)
    {
        //只有一个数的时候输出1
        if (n == 1)
        {
            printf("1");
        }
    
        //动态生成一个二维数组
        int **shouList = new int*[n];//开辟行
        for (int i = 0; i < n; i++)
            shouList[i] = new int[n]; //开辟列
    
        int max = n*n;
    
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                shouList[i][j] = max;
                max--;
            }
        }
    
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                printf("%4d", shouList[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    void Snake(int n)
    {
        //只有一个数的时候输出1
        if (n == 1)
        {
            printf("1");
        }
    
        //动态生成一个二维数组
        int **shouList = new int*[n];//开辟行
        for (int i = 0; i < n; i++)
            shouList[i] = new int[n]; //开辟列
    
        int row = 0, col = 0;
        int Filler = 1;
        int loop = 0;
    
        for (size_t loop = 0; loop < 2*n-1; loop++)
        {
            row = (loop < n) ? 0 : loop - (n - 1);
            col = (loop < n) ? loop : (n - 1);
            for (int j = row; j <= col; j++)
                (loop % 2) ? (shouList[j][loop - j] = Filler++) : (shouList[loop - j][j] = Filler++);
        }
    
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                printf("%4d", shouList[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    void Rotation(int n)
    {
        //只有一个数的时候输出1
        if (n == 1)
        {
            printf("1");
        }
    
        //动态生成一个二维数组
        int **shouList = new int*[n];//开辟行
        for (int i = 0; i < n; i++)
            shouList[i] = new int[n]; //开辟列
    
        int Filler = 1;
        int head = 0, tail = n-1;
        int row = 0, col = 0;
    
        for (int i = 0; i <= n / 2; i++)
        {
    
            if (head == tail)
            {
                shouList[head][tail] = Filler;
            }
            for (row = head; row < tail; row++)
            {
                shouList[row][col] = Filler++;
            }
            for (col = head; col < tail; col++)
            {
                shouList[row][col] = Filler++;
            }
            for (row = tail; row > head; row--)
            {
                shouList[row][col] = Filler++;
            }
            for (col = tail; col> head; col--)
            {
                shouList[row][col] = Filler++;
            }
            head++; 
            tail--;
            row = head;
            col = head;
        }
    
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                printf("%4d", shouList[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    int main()
    {
        int n = 0,choose = 0;
    
        cout << "Please input the dimension you want to Constructing the Matrix :" << endl;
        cin >> n;
        cout << "Please choose the Matrix:" << endl;
        cout << "1. Inversion ;" << endl;
        cout << "2. Snake ;" << endl;
        cout << "3. Rotation ;" << endl;
        cin >> choose;
    
        switch (choose)
        {
        case 1:
            Inversion(n);
            break;
        case 2:
            Snake(n);
            break;
        case 3:
            Rotation(n);
            break;
        default:
            cout << "Error!" << endl;
            break;
        }
    
        system("pause");
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路