헬리콥터 (반복 및 비반복)

25471 단어 비귀속
  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 }

좋은 웹페이지 즐겨찾기