• 螺旋阵(递归和非递归)


      1 #include <iostream>
      2 #include <cstdlib>
      3 #include <cstring>
      4 
      5 int **matrix;
      6 int value = 1;
      7 
      8 void recursion (int row, int col, int len)              // row, col代表本次左上角元素的位置, len为本次矩阵边长
      9 {
     10     if (len <= 0)
     11     {
     12         return;
     13     }
     14     
     15     int i = row, j = col;
     16     while (j < col + len)
     17     {
     18         matrix[i][j++] = value++;
     19     }
     20     
     21     j--;
     22     i++;
     23     
     24     while (i < row + len)
     25     {
     26         matrix[i++][j] = value++;
     27     }
     28     
     29     i--;
     30     j--;
     31     
     32     while (j >= col)
     33     {
     34         matrix[i][j--] = value++;
     35     }
     36     
     37     j++;
     38     i--;
     39     
     40     while (i > row)
     41     {
     42         matrix[i--][j] = value++;
     43     }
     44     
     45     i++;
     46     j++;
     47     
     48     recursion(i, j, len - 2);
     49 }
     50 
     51 void no_recursion ( int n )                    // n为整个矩阵的边长
     52 {    
     53     int i, j, len, col, row;
     54     for (i = 0, j = 0, len = n; len > 0; )
     55     {
     56         row = i;
     57         col = j;
     58         
     59         while (j < col + len)
     60         {
     61             matrix[i][j++] = value++;
     62         }
     63         
     64         j--;
     65         i++;
     66         
     67         while (i < row + len)
     68         {
     69             matrix[i++][j] = value++;
     70         }
     71         
     72         i--;
     73         j--;
     74         
     75         while (j >= col)
     76         {
     77             matrix[i][j--] = value++;
     78         }
     79         
     80         j++;
     81         i--;
     82         
     83         while (i > row)
     84         {
     85             matrix[i--][j] = value++;
     86         }
     87         
     88         i++;
     89         j++;
     90         
     91         len = len - 2;
     92     }
     93 }
     94 
     95 int main()
     96 {
     97     int N = 7, i, j;
     98     matrix = (int**) malloc(sizeof(int*) * N);
     99     
    100     for (i = 0; i < N; i++)
    101     {
    102         matrix[i] = (int*) malloc(sizeof(int) * N);
    103         memset(matrix[i], 0, sizeof(int) * N);
    104     }
    105     
    106     no_recursion(N);
    107     
    108     for (i = 0; i < N; i++)
    109     {
    110         for (j = 0; j < N; j++)
    111         {
    112             std::cout << matrix[i][j] << ' ';
    113         }
    114         std::cout << std::endl;
    115     }
    116     
    117     for (i = 0; i < N; i++)
    118     {
    119         free(matrix[i]);
    120     }
    121     
    122     free(matrix);
    123     
    124     return 0;
    125 }
      1 #include <iostream>
      2 #include <cstdlib>
      3 #include <cstring>
      4 
      5 int **matrix;
      6 int value = 1;
      7 
      8 void recursion (int row, int col, int len)              // row, col代表本次左上角元素的位置, len为本次矩阵边长
      9 {
     10     if (len <= 0)
     11     {
     12         return;
     13     }
     14     
     15     int i = row, j = col;
     16     while (j < col + len)
     17     {
     18         matrix[i][j++] = value++;
     19     }
     20     
     21     j--;
     22     i++;
     23     
     24     while (i < row + len)
     25     {
     26         matrix[i++][j] = value++;
     27     }
     28     
     29     i--;
     30     j--;
     31     
     32     while (j >= col)
     33     {
     34         matrix[i][j--] = value++;
     35     }
     36     
     37     j++;
     38     i--;
     39     
     40     while (i > row)
     41     {
     42         matrix[i--][j] = value++;
     43     }
     44     
     45     i++;
     46     j++;
     47     
     48     recursion(i, j, len - 2);
     49 }
     50 
     51 void no_recursion ( int n )                    // n为整个矩阵的边长
     52 {    
     53     int i, j, len, col, row;
     54     for (i = 0, j = 0, len = n; len > 0; )
     55     {
     56         row = i;
     57         col = j;
     58         
     59         while (j < col + len)
     60         {
     61             matrix[i][j++] = value++;
     62         }
     63         
     64         j--;
     65         i++;
     66         
     67         while (i < row + len)
     68         {
     69             matrix[i++][j] = value++;
     70         }
     71         
     72         i--;
     73         j--;
     74         
     75         while (j >= col)
     76         {
     77             matrix[i][j--] = value++;
     78         }
     79         
     80         j++;
     81         i--;
     82         
     83         while (i > row)
     84         {
     85             matrix[i--][j] = value++;
     86         }
     87         
     88         i++;
     89         j++;
     90         
     91         len = len - 2;
     92     }
     93 }
     94 
     95 int main()
     96 {
     97     int N = 7, i, j;
     98     matrix = (int**) malloc(sizeof(int*) * N);
     99     
    100     for (i = 0; i < N; i++)
    101     {
    102         matrix[i] = (int*) malloc(sizeof(int) * N);
    103         memset(matrix[i], 0, sizeof(int) * N);
    104     }
    105     
    106     no_recursion(N);
    107     
    108     for (i = 0; i < N; i++)
    109     {
    110         for (j = 0; j < N; j++)
    111         {
    112             std::cout << matrix[i][j] << ' ';
    113         }
    114         std::cout << std::endl;
    115     }
    116     
    117     for (i = 0; i < N; i++)
    118     {
    119         free(matrix[i]);
    120     }
    121     
    122     free(matrix);
    123     
    124     return 0;
    125 }
  • 相关阅读:
    常用的Dos命令
    关于CSS3
    数据渲染
    jQuery中的AJAX
    AJAX
    面向对象3
    克隆对象、对象继承
    面向对象2
    面向对象1
    面向对象
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2819916.html
Copyright © 2020-2023  润新知