Clockwise filling of two-dimensional arrays (implemented in C)

Source: Internet
Author: User

As shown in: For any given n, print the following data clockwise:

3

1 2 3

6 4

5

----------------------------------

4

1 2 3 4

9 10 5

8 6

7

----------------------------------

5

1 2 3 4 5

12 13 14 6

11 15 7

10 8

9

...

...

...

For such data filling, the filling principle is similar to the clockwise filling problem of a full two-dimensional array. Next we will analyze the data filling problem of such an array.

Based on the clockwise filling of the question, we can divide the problem into clockwise solving:

<1> think of this array as an equi triangle and separate it from the outer self. For example, when n = 5,

1 2 3 4 5 and

12 6 13 14

11 7 15

10 8

9

These two equi triangles are used for analysis.

<2> Fill each equi-edge triangle in clockwise direction.

1, the elements of the first line are a [k] [J] = A [k] [J-1] + 1;

2, the elements on the diagonal of the pair are a [I] [S + 2-k-i] = A [I-1] [S + 3-k-i] + 1;

3. Fill in a [I-1] [k] = A [I] [k] + 1 from the last line in sequence for the elements in the first column;

The most complicated issue is the filling of sub-diagonal elements. In fact, it is not difficult to find that the subscript of the element on the diagonal is S + 2-k;

(S is the number of elements in array a [] [], n is the number of elements in the first line of the K equi triangle, and K is the K equi triangle currently being filled)

 

I will post my own program for you to share. If anything is inappropriate, you are welcome to criticize and advise.

 

# Include <stdio. h> <br/> # define N 21 <br/> int s; <br/> int a [n] [N]; </P> <p> void fun (int n, int K) <br/> {<br/> int I, j; <br/> If (k> 1) <br/> A [k] [k] = A [k] [k-1] + 1; <br/> else <br/> A [k] [k] = 1; <br/> // fill in rows <br/> for (j = k + 1; j <= N + k-1; j + +) <br/> A [k] [J] = A [k] [J-1] + 1; <br/> // sub-diagonal padding <br/> for (I = k + 1; I <= N + k-1; I ++) <br/> A [I] [S + 2-k-i] = A [I-1] [S + 3-k-i] + 1; <br/> // column filling <br/> for (I = N + k-1; I> K + 1; I --) <br/> A [I-1] [k] = A [I] [k] + 1; </P> <p >}< br/> // output data <br/> void print (int n) <br/>{< br/> int I, J; <br/> for (I = N; I> 0; I --) <br/> {<br/> for (j = 1; j <= I; j ++) <br/> printf ("% 4D", a [n-I + 1] [J]); <br/> printf ("/N "); <br/>}< br/> void main () <br/>{< br/> int X, I; <br/> I = 1; <br/> scanf ("% d", & S); <br/> X = s; <br/> DO <br/> {<br/> fun (X, I); <br/> X = X-3; <br/> I ++; <br/>}while (x> 0); <br/> Print (s); <br/>}</P> <p>

 

 

 

 

 

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.