Array spiral Printing

Source: Internet
Author: User
Package Su. interview; import utils.com. matrixer;/*** spiral print sequence ** @ author toy **/public class array_01 {/*** print the southeast and northwest in the outward direction -- southeast and northwest .. n-> N-1-> N-1-> N-2 ** @ return */Public int [] [] print_01 (int n) {int [] [] matrix = new int [N] [N]; int I = 0; Int J = 0; int num = 1; while (n> 0) {// East system. out. println ("A:" + I + "" + J); For (int K = 0; k <n; k ++) {matrix [I] [J] = num; num ++; j ++;} j --; I ++; // South system. out. println ("B:" + I + "" + J); For (int K = 0; k <n-1; k ++) {matrix [I] [J] = num; num ++; I ++;} I --; j --; // West system. out. println ("C:" + I + "" + J); For (int K = 0; k <n-1; k ++) {matrix [I] [J] = num; num ++; j --;} J ++; I --; // North system. out. println ("D:" + I + "" + J); For (int K = 0; k <n-2; k ++) {matrix [I] [J] = num; num ++; I --;} I ++; j ++; n = N-2;} return matrix ;} /*** print from the inside out (clockwise) North East South West -- north east south west .. n-> N-1-> N ** @ return */Public int [] [] print_02 (int n) {int [] [] matrix = new int [N] [N]; int I = n/2; Int J = (n-1)/2; int num = 1; int step = 2; while (Num <= N * n) {system. out. println ("step:" + step); // North system. out. println ("D:" + I + "" + J); For (int K = 0; k <step & (Num <= N * n); k ++) {matrix [I] [J] = num; num ++; I --;} I ++; j ++; // East system. out. println ("A:" + I + "" + J); For (int K = 0; k <(step-1) & (Num <= N * n ); k ++) {matrix [I] [J] = num; num ++; j ++;} j --; I ++; // South system. out. println ("B:" + I + "" + J); For (int K = 0; k <step & (Num <= N * n); k ++) {matrix [I] [J] = num; num ++; I ++;} I --; j --; // West system. out. println ("C:" + I + "" + J); For (int K = 0; k <step & (Num <= N * n); k ++) {matrix [I] [J] = num; num ++; j --;} J ++; I --; step = Step + 1;} return matrix ;} /*** @ Param ARGs */public static void main (string [] ARGs) {int n = 3; int [] [] m; array_01 arr = new array_01 (); M = arr. print_01 (n); matrixer. show (m, n, n); M = arr. print_02 (n); matrixer. show (m, n, n );}}

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.