顺时针打印数组,美丽版
今天看到一段代码,顺时针打印数组。写得太美了,简洁明了,几乎不用任何注释。
转过来,原来是java版的,我改成了C++版。
1 void PrintMatrixClockWise() 2 { 3 int m = 5; 4 int n = 4; 5 int val[4][5] = 6 { 7 {1, 2, 3, 4, 5 }, 8 {6, 7, 8, 9, 1 }, 9 {11, 12, 13, 14, 15}, 10 {16, 17, 18, 19, 20} 11 }; 12 13 int colMin = 0; 14 int colMax = m; 15 int rowMin = 0; 16 int rowMax = n; 17 18 while(colMin < colMax && rowMin < rowMax) 19 { 20 for(int i = colMin; i < colMax; i++) 21 cout << (val[rowMin][i]) << " "; 22 cout << endl; 23 rowMin++; 24 25 for(int i = rowMin; i < rowMax; i++) 26 cout << (val[i][colMax-1]) << " "; 27 cout << endl; 28 colMax--; 29 30 for(int i = colMax-1; i >= colMin; i--) 31 cout << (val[rowMax-1][i]) << " "; 32 cout << endl; 33 rowMax--; 34 35 for(int i = rowMax-1; i >= rowMin; i--) 36 cout << (val[i][colMin]) << " "; 37 cout << endl; 38 colMin++; 39 } 40 }