Non-recursive algorithms cleverly solve the "Spiral square matrix" Problem

Source: Internet
Author: User

Define the Order N of the spiral matrix, store the spiral matrix in a two-dimensional array of n × n, and print it out.Note: Please respect the individual's labor results, and indicate the source for reprinting.

The implementation code is as follows:

/*************************************** * ********************************** File Name: helixmatrix. c ** created on: 2012-8-15 ** Author: Martin (p000034) ** contact information: Ma.Martin153@gmail.com ** Description: Order n of a custom spiral matrix, store the spiral square matrix in the ** n × n two-dimensional array and print it out. ** Modification: No *** statement: this procedure is only for study and communication and cannot be used for commercial purposes without the author's authorization. ** otherwise, the legal liability is reserved. **************************************** * *********************************/# Include <stdio. h> // The Order of the custom spiral matrix # define n 5int main (void) {int index; // The Int maxindex of the temporary count unit; // The upper bound of the temporary count unit int temprow; // The Int templine of the temporary count unit int maxtemp; // The Upper Bound int tempshowline of the temporary count unit int tempshowline; // display the temporary row counting unit int tempshowrow of the spiral matrix; // The temporary column counting unit int number = 0 used to display the spiral matrix; // accumulators int matrix [N] [N] = {0}; // stores the two-dimensional array of the spiral matrix maxindex = (n + 1)/2-1 ); // cyclically, assign a value to the array for (Index = 0; index <= maxindex; index ++) {temprow = index; templine = index; maxtemp = N-(Index * 2 + 1) + index; // left vertical loop for (; temprow <maxtemp; temprow ++) {matrix [temprow] [templine] = (++ number);} // lower horizontal loop for (; templine <maxtemp; templine ++) {matrix [temprow] [templine] = (++ number);} // right vertical loop for (; temprow> index; temprow --) {matrix [temprow] [templine] = (++ number);} // The upper horizontal loop for (; templine> index; templine --) {matrix [temprow] [templine] = (++ number) ;}// if n is an odd number if (1 = n % 2) {matrix [temprow] [templine] = (++ number) ;}else {;}// display the spiral matrix printf ("% d level spiral matrix: \ n ", n); For (tempshowrow = 0; tempshowrow <n; tempshowrow ++) {for (tempshowline = 0; tempshowline <n; tempshowline ++) {printf ("%-6D", matrix [tempshowrow] [tempshowline]);} printf ("\ n");} return 0 ;}

When the order of the spiral matrix is 5, It is shown as follows:

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.