螺旋输出N*N矩阵

#include <iostream>

#include <iomanip>

using namespace std;

void func(int n){

    static const int dir[][2]={{0,1},{1,0},{0,-1},{-1,0}};

    const int size=n+2;

    int **ver=new int *[size];

    for (int i=0;i<size;++i)

    {

       ver[i]=new int[size];

       if (i==0||i==size-1)

       {

           memset(ver[i],-1,size*sizeof(int));

       }

       else{

           memset(ver[i],0,size*sizeof(int));

           ver[i][0]=-1;

           ver[i][size-1]=-1;

       }     

    }

    int iIndex=0;

    int max=n*n;

    int cnt=1;

    int i=1,j=1;

    while (cnt<=max)

    {

       while (ver[i][j]==0)

       {

           ver[i][j]=cnt;

           i+=dir[iIndex][0];

           j+=dir[iIndex][1];

           ++cnt;

       }

       i-=dir[iIndex][0];

       j-=dir[iIndex][1];

       if (cnt>max)

       {

           break;

       }

       iIndex=(iIndex+1)%4;

       i+=dir[iIndex][0];

       j+=dir[iIndex][1];

       while (ver[i][j]!=0)

       {

           iIndex=(iIndex+1)%4;

           i+=dir[iIndex][0];

           j+=dir[iIndex][1];

       }

    }

 

 

    for (int i=1;i<size-1;++i)

    {

       for (int j=1;j<size-1;++j)

       {

           cout<<setw(6)<<ver[i][j];

       }

       cout<<endl;

    }

}

int main(){

    int n;

    cin>>n;

    func(n);  

}

10
     1     2     3     4     5     6     7     8     9    10
    36    37    38    39    40    41    42    43    44    11
    35    64    65    66    67    68    69    70    45    12
    34    63    84    85    86    87    88    71    46    13
    33    62    83    96    97    98    89    72    47    14
    32    61    82    95   100    99    90    73    48    15
    31    60    81    94    93    92    91    74    49    16
    30    59    80    79    78    77    76    75    50    17
    29    58    57    56    55    54    53    52    51    18
    28    27    26    25    24    23    22    21    20    19

 
下面是一种实现 n * n 螺旋矩阵的方法: 1. 定义一个 n * n 的二维数组。 2. 使用变量 row 和 col 记录当前要填充的位置。 3. 使用变量 num 记录当前要填充的数字。 4. 使用变量 direction 记录当前遍历的方向,初始值为 0。 5. 使用变量 count 记录已经填充的数字个数,初始值为 0。 6. 不断循环,直到填充完所有数字: 1. 根据当前遍历方向,依次填充一行或一列的数字,同时更新 row 和 col。 2. 如果到达边界或者已经填充过该位置,就改变遍历方向,同时更新 row 和 col。 3. 将计数器 count 加 1。 7. 输出生成的螺旋矩阵。 下面是示例代码: ```c #include <stdio.h> int main() { int n; printf("请输入矩阵的大小 n:"); scanf("%d", &n); int matrix[n][n]; int row = 0, col = 0, direction = 0, num = 1, count = 0; while (count < n * n) { matrix[row][col] = num; num++; count++; switch (direction) { case 0: if (col == n - 1 || matrix[row][col + 1] != 0) { direction = 1; row++; } else { col++; } break; case 1: if (row == n - 1 || matrix[row + 1][col] != 0) { direction = 2; col--; } else { row++; } break; case 2: if (col == 0 || matrix[row][col - 1] != 0) { direction = 3; row--; } else { col--; } break; case 3: if (row == 0 || matrix[row - 1][col] != 0) { direction = 0; col++; } else { row--; } break; } } printf("生成的螺旋矩阵为:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%-4d", matrix[i][j]); } printf("\n"); } return 0; } ``` 输入输出示例: ``` 请输入矩阵的大小 n:5 生成的螺旋矩阵为: 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值