Programming algorithm-Rotation matrix code (C) __ajax

Source: Internet
Author: User
Rotation Matrix Code (C)


This article address: Http://blog.csdn.net/caroline_wendy


Output rotation matrix , so that the matrix is a diagonal spiral up , after the output rule is determined, it can be judged, the rising rule is that the row and column added to the fixed value .

So we use the method of two loops and the upper and lower matrices to separate output.

Such as:

  1   2   6   7
  3   5
 8 4 9  25 (in)

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 = m;
	int A[max][max];

	int min = 1;
	int max = n*n;

	The last half 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++) {
		for (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
  3   5  8 4 9 22  25 (in)




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.