Rotate and print order n matrices clockwise (0th questions) and matrices 4th
Question requirements
Problem description: clockwise rotation of the n-order matrix
Example input: 4
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Sample output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Solution
First, establishPrint in circlesFirst print the outermost circle, then print the circle in the middle, and so on. During printing, the rotation traversal is divided into four actions: from left to right, from top to bottom, from right to left, and from bottom to top. Each cycle is marked with two elements on the diagonal corner to guide the direction of the cycle, print a circle, and thenInward contractionPrint the next lap.
Source code example and Result Display
Summary
You can also write this question as follows:Tail recursionThe form, however, is essentially the same, that is, printing in a circle and splitting the printing action into four directions.