蛇形数组(理解)c++

文章介绍了如何使用C++编程语言创建动态二维数组,并通过四个方向的循环实现环绕型填充。代码展示了从左到右、上到下、右到左、下到上的填充过程,以及如何在填充完成后释放内存。
摘要由CSDN通过智能技术生成

打印一个环绕型的数组,🤔,那么元素应该先从左到右(看具体情况),到末列时候在从上往下,到末行在从右往左 and so on;

首先,定义一个动态二维数组, int **arr=new int *[rows]; for(int i=0;i<rows;i++){ arr[i]=new int [line];rows ,line 都为手动输入的;以便可以实现各种长度的;然后就可以开始去给数组中每个元素赋值了9ad7ab10b03c4620a7a4abdb872bf620.png

int startA=0,startB=0,endA=rows-1,endB=line-1; int i=0,count=0(计数)首先从左到右for(i=startB;i<endB;i++){arr[startA][i]=++count;}

从上到下 for(i=startA;i<=endA;i++){arr[i][endB]=++count;}. 从右到左 for(i=endB-1;i>=startB;i--){arr[endA][i]=++count;}从下到上 for(i=endA-1;i>=startA;i--){arr[i][startB]=++count;}

形成一个矩形后每次循环时初始值会减一,并且末尾值也会减一;即endA--;endB--;startA++;startB++;

当然条件应该是while(count<row*line);

最后,动态数组空间要回收;

  for(int i=0;i<rows;i++)
    {
        delete [] arr[i];
    }
    delete [] arr;
 

 

 

 0cdacaf4ec4c4a3ca9b59086cc66b39c.png

 

  完整代码:#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int rows;
    cout << "Input a rows :";
    cin>>rows;
    
    int line;
    cout << "Input a line :";
    cin>>line;
    
    int **arr=new int* [rows];
    for(int i=0;i<rows;i++)
    {
        arr[i]=new int [line];
    }
    int i=0;
    
    int startA=0;
    int startB=0;
    int endA=rows-1;
    int endB=line-1;
    int count=0;
    
    while(count <rows*line)
    {
        for(i=startB;i<=endB;i++)
        {
            arr[startA][i]=++count;
            
        }
        for(i=startA+1;i<=endA;i++)
        { arr[i][endB]=++count;
            
        }
        for(i=endB-1;i>=startB;i--)
        {
            arr[endA][i]=++count;
            
        }
        for(i=endA-1;i>=startA+1;i--)
        {arr[i][startB]=++count;
            
        }
        startA++;
        startB++;
        endA--;
        endB--;
    }
 
    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<line;j++)
        {
            cout <<setw(4)<< arr[i][j]<<" ";
        }
        cout <<endl;
    }
    for(int i=0;i<rows;i++)
    {
        delete [] arr[i];
    }
    delete [] arr;
    
    return 0;
}

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用递归实现蛇形数组的C语言代码: ```c #include <stdio.h> void snake_array(int n, int row, int col, int arr[][n], int num) { if (num > n * n) { // 递归结束条件 return; } // 向右走 while (col < n && !arr[row][col]) { arr[row][col++] = num++; } col--; row++; // 向下走 while (row < n && !arr[row][col]) { arr[row++][col] = num++; } row--; col--; // 向左走 while (col >= 0 && !arr[row][col]) { arr[row][col--] = num++; } col++; row--; // 向上走 while (row >= 0 && !arr[row][col]) { arr[row--][col] = num++; } row++; col++; // 递归调用 snake_array(n, row, col, arr, num); } int main() { int n; printf("Enter the size of the matrix: "); scanf("%d", &n); int arr[n][n]; // 初始化数组 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { arr[i][j] = 0; } } snake_array(n, 0, 0, arr, 1); // 输出数组 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d\t", arr[i][j]); } printf("\n"); } return 0; } ``` 该程序首先要求用户输入矩阵的大小,然后初始化一个二维数组,并将其作为参数传递给`snake_array`函数。`snake_array`函数用于递归地生成蛇形数组,其中`num`参数用于记录当前应该填入的数字。该函数的实现过程如下: 1. 如果`num`大于矩阵中元素的总数,则递归结束。 2. 向右走,直到遇到已经填入数字的位置或者到达矩阵的右边界。 3. 向下走,直到遇到已经填入数字的位置或者到达矩阵的下边界。 4. 向左走,直到遇到已经填入数字的位置或者到达矩阵的左边界。 5. 向上走,直到遇到已经填入数字的位置或者到达矩阵的上边界。 6. 递归调用`snake_array`函数,继续填充矩阵。 最后,程序输出生成的蛇形数组

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JXF111400

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值