JZ-C-20

剑指offer第二十题:顺时针打印矩形

  1 //============================================================================
  2 // Name        : JZ-C-20.cpp
  3 // Author      : Laughing_Lz
  4 // Version     :
  5 // Copyright   : All Right Reserved
  6 // Description : 顺时针打印矩形
  7 //============================================================================
  8 
  9 #include <iostream>
 10 #include <stdio.h>
 11 using namespace std;
 12 
 13 void PrintMatrixInCircle(int** numbers, int columns, int rows, int start);
 14 void printNumber(int number);
 15 void PrintMatrixClockwisely(int ** numbers, int columns, int rows) {
 16     if (numbers == NULL || columns <= 0 || rows <= 0) {
 17         return;
 18     }
 19     int start = 0;
 20     //每次循环打印一个圆
 21     while (start * 2 < columns && start * 2 < rows) { //可循环打印的条件
 22         PrintMatrixInCircle(numbers, columns, rows, start);
 23         start++;
 24     }
 25 }
 26 void PrintMatrixInCircle(int** numbers, int columns, int rows, int start) {
 27     int endX = columns - 1 - start;
 28     int endY = rows - 1 - start;
 29     //从左到右打印
 30     for (int i = start; i <= endX; i++) {
 31         printNumber(numbers[start][i]);
 32     }
 33     //从上到下打印
 34     if (start < endY) {
 35         for (int i = start + 1; i <= endY; i++) {
 36             printNumber(numbers[i][endX]);
 37         }
 38     }
 39     //从右到左打印
 40     if (start < endX && start < endY) {
 41         for (int i = endX - 1; i >= start; i--) {
 42             printNumber(numbers[endY][i]);
 43         }
 44     }
 45     //从下到上打印
 46     if (start < endY - 1 && start < endX) {
 47         for (int i = endY - 1; i >= start + 1; i--) {
 48             printNumber(numbers[i][start]);
 49         }
 50     }
 51 }
 52 void printNumber(int number) {
 53     printf("%d\t", number);
 54 }
 55 // ====================测试代码====================
 56 void Test(int columns, int rows)
 57 {
 58     printf("Test Begin: %d columns, %d rows.\n", columns, rows);
 59 
 60     if(columns < 1 || rows < 1)
 61         return;
 62 
 63     int** numbers = new int*[rows];
 64     for(int i = 0; i < rows; ++i)
 65     {
 66         numbers[i] = new int[columns];
 67         for(int j = 0; j < columns; ++j)
 68         {
 69             numbers[i][j] = i * columns + j + 1;
 70         }
 71     }
 72 
 73     PrintMatrixClockwisely(numbers, columns, rows);
 74     printf("\n");
 75 
 76     for(int i = 0; i < rows; ++i)
 77         delete[] (int*)numbers[i];
 78 
 79     delete[] numbers;
 80 }
 81 
 82 int main(int argc, char** argv)
 83 {
 84     /*
 85     1
 86     */
 87     Test(1, 1);
 88 
 89     /*
 90     1    2
 91     3    4
 92     */
 93     Test(2, 2);
 94 
 95     /*
 96     1    2    3    4
 97     5    6    7    8
 98     9    10   11   12
 99     13   14   15   16
100     */
101     Test(4, 4);
102 
103     /*
104     1    2    3    4    5
105     6    7    8    9    10
106     11   12   13   14   15
107     16   17   18   19   20
108     21   22   23   24   25
109     */
110     Test(5, 5);
111 
112     /*
113     1
114     2
115     3
116     4
117     5
118     */
119     Test(1, 5);
120 
121     /*
122     1    2
123     3    4
124     5    6
125     7    8
126     9    10
127     */
128     Test(2, 5);
129 
130     /*
131     1    2    3
132     4    5    6
133     7    8    9
134     10   11   12
135     13   14   15
136     */
137     Test(3, 5);
138 
139     /*
140     1    2    3    4
141     5    6    7    8
142     9    10   11   12
143     13   14   15   16
144     17   18   19   20
145     */
146     Test(4, 5);
147 
148     /*
149     1    2    3    4    5
150     */
151     Test(5, 1);
152 
153     /*
154     1    2    3    4    5
155     6    7    8    9    10
156     */
157     Test(5, 2);
158 
159     /*
160     1    2    3    4    5
161     6    7    8    9    10
162     11   12   13   14    15
163     */
164     Test(5, 3);
165 
166     /*
167     1    2    3    4    5
168     6    7    8    9    10
169     11   12   13   14   15
170     16   17   18   19   20
171     */
172     Test(5, 4);
173 
174     return 0;
175 }

 

posted @ 2016-06-12 15:05  回看欧洲  阅读(180)  评论(0编辑  收藏  举报