Rotate and print order n matrices clockwise (0th questions) and matrices 4th

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.