Programming Algorithm-rotating matrix code (c)

Source: Internet
Author: User
Rotating matrix code (c)


Address: http://blog.csdn.net/caroline_wendy


OutputRotation matrixTo make the MatrixDiagonal spiral riseAfter the output rule is determined, you can determine that the rising rule is,Add rows and columns as values.

Therefore, two loops are used, and the upper and lower matrices are used to separate the output.

For example:

  1   2   6   7  15  3   5   8  14  16  4   9  13  17  22 10  12  18  21  23 11  19  20  24  25

Code:

/** Main. CPP ** created on: 2014.6.12 * Author: Spike * // * eclipse CDT, GCC 4.8.1 */# include <stdio. h> void output (int n) {If (n <0) return; const int max = 100; int A [Max] [Max]; int min = 1; int max = N * n; // upper matrix for (INT I = 0; I <n; I ++) {for (Int J = 0; j <I + 1; j ++) if (I % 2 = 0) {A [I-j] [J] = min ++; A [n-1-i + J] [n-1-j] = max --;} else {A [J] [I-j] = min ++; A [n-1-j] [n-1-i + J] = max -- ;}for (INT I = 0; I <n; I ++) { (Int J = 0; j <n; j ++) printf ("% 3d % C", a [I] [J], j = n-1? '\ N': '') ;}} int main () {output (5); Return 0 ;}


Output:

  1   2   6   7  15  3   5   8  14  16  4   9  13  17  22 10  12  18  21  23 11  19  20  24  25




Programming Algorithm-rotating matrix code (c)

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.