[Meet time] sword finger offer-clockwise print matrix Java

Source: Internet
Author: User

Title Description:

Enter a matrix that prints each number in a clockwise order from outward, for example, if you enter the following matrix:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

The word 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 is printed in turn.

Input:

The input may contain multiple test samples, for each test case,

The first line of input includes two integers m and n (1<=m,n<=1000): Represents the number of dimensions of the matrix as M row n columns.

The next M-line, each row consists of n integers, representing the elements of the matrix, where each element A has a value range of (1<=a<=10000).

Output:

For each test case, output one line,

Print each number in a clockwise order from the outside, with a space behind each number.

Sample input:
4 41 2 3 45 6 7 89 10 11 1213 14 15 16
Sample output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Java Code Implementation
1  Public classCircleoutput {2      Public Static voidPrintmatrixincircle (int[] Narr,intRowsintcolumns,3             intNstart) {4         //Nendx is the right column number5         intNendx = columns-1-Nstart;6         //Nendy is the bottom line number7         intNendy = rows-1-Nstart;8 9         //print a line from left to rightTen System.out.println (); OneSystem.out.println ("Print a line from left to right"); A          for(inti = Nstart; I <= nendx; i++) { -          -System.out.print (Narr[nstart][i] + "\ T"); the         } -  -         //print a column from top to bottom -          +         if(Nendy >Nstart) { - System.out.println (); +System.out.println ("Print a column from top to bottom"); A              for(intj = Nstart + 1; J <= Nendy; J + +) { at                  -System.out.print (Narr[j][nendx] + "\ T"); -             } -         } -         //print a line from right to left -          in         if(Nendx > Nstart && nendy >Nstart) { - System.out.println (); toSystem.out.println ("Print one line from right to left"); +              for(intK = nEndX-1; K >= Nstart; k--) { -System.out.print (Narr[nendy][k] + "\ T"); the             } *         } $         //print a column from bottom to topPanax Notoginseng          -         if(NEndY-1 > Nstart && nendx >Nstart) { the System.out.println (); +System.out.println ("Print a column from bottom to top"); A              for(intp = nEndY-1; P >= Nstart + 1; p--) { theSystem.out.print (Narr[p][nstart] + "\ T"); +             } -         } $  $     } -  -      Public Static voidPrintint[] Narr,intRowsintcolumns) { the         if(Narr = =NULL|| Rows <= 0 | | Columns <= 0) { -             return;Wuyi         } the         intNstart = 0; -          while(Rows > (Nstart * 2) && columns > (Nstart * 2))) { Wu printmatrixincircle (Narr, rows, columns, nstart); -nstart++; About         } $     } -  -      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub A         int[] nMatrix1 = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12 }, +{13, 14, 15, 16 } }; thePrint (NMATRIX1, 4, 4);  -/* $         int[] nMatrix2 = {{1,2,3,4}}; theCircleoutput.print (nMatrix2, 1, 4); the          the         int[] NMatrix3 ={{1,2,3,4,5},{6,7,8,9,10}}; theCircleoutput.print (NMatrix3, 2, 5); -          in         int[] nMatrix4 = {{1},{2},{3},{4}}; theCircleoutput.print (NMATRIX4, 4, 1);
*/ the } About}

Output Result:

print a row from left to right 1    2    3    4 print     a column from top to bottom 8        -from right to left-prints a row of    14        print a column from bottom to top 9    5     print a row from left to right 6    7     print a column from top to bottom    print a row from right to left 10



[Meet time] sword finger offer-clockwise print matrix Java

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.