Counter-clockwise print matrix, counter-clockwise Matrix

Source: Internet
Author: User

Counter-clockwise print matrix, counter-clockwise Matrix

The life of graduate students is boring. Recently I want to go out and look for internships. I will review the data structure. I did not review the data structure in my freshman year. I was despised during the interview. When I saw a senior student, it should be the test question of Kingsoft: 

To a matrix, print the matrix counter-clockwise, for example:

1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
Print it out in a counter-clockwise manner: 1 2 3 4 5... 14 15 16

At first glance, this question is very simple, that is, it can be done in two cycles. In fact, it is not as simple as you think, because every cycle changes. I have referred to some ideas on solving problems on the Internet. Now let's talk about my ideas.

1. Define a direction d = (d + 1) % 4, then d =, 2 and 3 indicate four directions

2. print the current row col: k-> j of the matrix, column row: m-> n, initial condition: col = 0, row = 0, k = 0, j = 3, m = 0, n = 3

3. When the first column is printed, k + = 1, col = n

4. When the last row is printed, n-= 1, row = j

5. Print the last column, j-= 1, col = m

6. Print the first line, m + = 1, row = k

...

7. termination conditions: m = n & k = j

The following shows the code I wrote in java:

Int [] [] p = {,}, {,}; int col = 0, row = 0; // The current column and row int m = 0, n = 3, k = 0, j = 3; // col: m-> n row: k-> j int d =-1; // indicates the direction while (true) {d = (d + 1) % 4; switch (d) {case 0: for (int I = m; I <= n; I ++) {System. out. print (p [I] [row] + "");} col = n; k + = 1; // prevent repeated break; case 1: for (int I = k; I <= j; I ++) {System. out. print (p [col] [I] + "");} row = j; n-= 1; // prevent repeated break; case 2: for (int I = n; i> = m; I --) {System. out. print (p [I] [row] + "");} col = m; j-= 1; break; case 3: for (int I = j; i> = k; I --) {System. out. print (p [col] [I] + "");} row = k; m + = 1; break; default: break ;} if (m = n & k = j) {break ;}}


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.