ACM snake Matrix

Source: Internet
Author: User

It took me one morning to create an ACM question at Tongji University. The question is:
Snake row matrix
Problem
A snake matrix is a matrix triangle arranged in sequence by natural numbers starting from 1.

Input
This question contains multiple groups of data, each of which is composed of a positive integer n. (N is not greater than 100)

Output
For each group of data, a matrix of N rows is output. Do not empty rows between the two sets of outputs.

The numbers in the same row in the matrix triangle are separated by a space. Do not use any extra space at the end of the line.

Sample Input
5

Sample output
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11

At first, I thought it was strange. It seems that this is not the case for shexing. If you care about it, you can do it. But it is really tiring to do this kind of mathematical problem. Now I feel more and more that my mathematics is so bad, depressed! ~ ''~~~ '
But it is still "difficult" to complete:

# Include
Int main ()
{
Int A [20] [20];
Int I, n, m, X, Y, num = 1;
Printf ("input N:/N ");
Scanf ("% d", & N );
For (I = 1; I <= N; I ++) // assign a number to a two-dimensional array
{
X = I; y = 1;
For (int count = 1; count <= I; count ++)
{
A [x] [Y] = num;
Num ++;
X --;
Y ++;
}
}
For (x = 1; x <= N; X ++) // print the Array
{
For (y = 1; y <= N-x + 1; y ++)
Printf ("% d", a [x] [Y]);
Printf ("/N ");

}
Return 0;
}

But I didn't expect the code to be so simple, that is, the for loop won't make me turn around for a long time. When I got up in the morning, I saw another question:

Number in The Matrix. When the N * n matrix is given, it is required to fill in the following forms with the program:
// ① Inverted fill, for example, n = 5 ② filled in by the snake ③ filled in by the rotation
//
// ┌ ── ┬ ── ┐
// │ 25 │ 24 │ 23 │ 22 │ 21 │ 1 │ 3 │ 4 │ 10 │ 11 │ 1 │ 16 │ 15 │ 14 │ 13 │
// ├ ── ┼ ── ┤
// │ 20 │ 19 │ 18 │ 17 │ 16 │ 2 │ 5 │ 9 │ 12 │ 19 │ 2 │ 17 │ 24 │ 23 │ 12 │
// ├ ── ┼ ── ┤
// │ 15 │ 14 │ 13 │ 12 │ 11 │ 6 │ 8 │ 13 │ 18 │ 20 │ 3 │ 18 │ 25 │ 22 │ 11 │
// ├ ── ┼ ── ┤
// │ 10 │ 9 │ 8 │ 7 │ 6 │ 7 │ 14 │ 17 │ 21 │ 24 │ 4 │ 19 │ 20 │ 21 │ 10 │
// ├ ── ┼ ── ┤
// │ 5 │ 4 │ 3 │ 2 │ 1 │ 15 │ 16 │ 22 │ 23 │ 25 │ 5 │ 6 │ 7 │ 8 │ 9 │
// └ ── ┴ ── ┘

Digging! In the middle, This is the snake line array that I have seen before. I will not talk about it before. In the middle, this road is just a bend than I did above, and the direction of the circle has changed, however, the above is in the same direction, but I have made the best effort:
# Include
Int main ()
{
Int A [20] [20];
Int I, n, m, X, Y, num = 1;
Printf ("input N:/N ");
Scanf ("% d", & N );

For (I = 1; I <= N; I ++) // assign the number to the upper triangle of the snake Array
{
If (I % 2 = 1)
{
X = 1;
Y = I;
For (int count = 1; count <= I; count ++)
{
A [x] [Y] = num;
Num ++;
X ++;
Y --;
}
}
Else
{
X = I;
Y = 1;
For (int count = 1; count <= I; count ++)
{
A [x] [Y] = num;
Num ++;
X --;
Y ++;
}
}
}
Num = N * N;
For (I = N; I> 1; I --) // assign the number to the lower triangle of the snake Array
{
If (I % 2 = 1)
{
X = N;
Y = I;
For (int count = 1; count <= N + 1-I; count ++)
{
A [x] [Y] = num;
Num --;
X --;
Y ++;
}
}
Else
{
X = I;
Y = N;
For (int count = 1; count <= N + 1-I; count ++)
{
A [x] [Y] = num;
Num --;
X ++;
Y --;
}
}
}
 
For (x = 1; x <= N; X ++) // print the Array
{
For (y = 1; y <= N; y ++)
Printf ("% d", a [x] [Y]);
Printf ("/N ");
}
Return 0;
}

 

To be honest, I can see that these nested loop headers are all big. Even if I did it myself, the answer is correct. I don't want to go back and read it again, I already have a lot of brain cells ~~~~ '

This course is very important because it focuses on thinking and consideration. It is applicable to any language. Now I have worked hard, as you can see from the above several so-called ACM questions (in fact, it is not that simple), we can see that our mathematics and algorithms are really weak. We can only continue to do the questions and will gradually accumulate...

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.