# 100 examples of C programming (29): Latin square

Time：2021-5-5

#### Example 29   Latin square

Problem description

A Latin square matrix of order NxN is constructed so that the numbers 1 to N in each row and column of the square matrix appear only once. If n = 4:

1 2 3 4

2 3 4 1

3 4 1 2

4 1 2 3

Input format

A positive integer n (2 < = n < = 9).

Output format

The generated square matrix of order n * n.

sample input

4

sample output

1 2 3 4

2 3 4 1

3 4 1 2

4 1 2 3

##### (1) Programming ideas.

By observing the given example, we can find that if the number of the first column and the number of the last column in each row are connected to form a ring, then the ring is composed of 1 to n sequence; For line I, the starting number of this ring is I. According to this rule, it is easy to write the program.

##### (2) Source program.

#include

int main()

{

int n;

scanf(“%d”,&n);

int i,k,t;

for(i=0; i

{

t=i%n;    // Determine the value of the first element of the i-th row of the Latin square matrix

for(k=0; k

printf(“%d “,(k+t)%n+1);

printf(“\n”);

}

return 0;

#### Exercise 29

##### 29-1   Odd order cube

This question is selected from the OJ question bank of Hangzhou University of Electronic Science and technology（ http://acm.hdu.edu.cn/showproblem.php?pid=1998 ）

Problem Description

The elements of an n-order square matrix are 1,2,…, n ^ 2. The sum of elements in each row, column and two diagonal lines is equal. Such a square matrix is called magic cube. When n is odd, we have a construction method, which is called “upper right”. For example, the magic cube when n = 3, 5, 7 is given below.

3

8 1 6

3 5 7

4 9 2

5

17 24 1 8 15

23 5 7 14 16

4 6 13 20 22

10 12 19 21 3

11 18 25 2 9

7

30 39 48 1 10 19 28

38 47 7 9 18 27 29

46 6 8 17 26 35 37

5 14 16 25 34 36 45

13 15 24 33 42 44 4

21 23 32 41 43 3 12

22 31 40 49 2 11 20

The number in the middle of the first line is always 1, the number in the middle of the last line is n ^ 2, and his right side is 2. From these three magic cubes, you can see what “top right” means.

Input

It contains multiple groups of data. First, enter t to indicate that there are t groups of data. One row of each group of data shows that n (3 < = n < = 19) is an odd number.

Output

For each group of data, output n magic cube, each number occupies 4 spaces, right aligned

Sample Input

2

3

5

Sample Output

8   1   6

3   5   7

4   9   2

17  24   1   8  15

23   5   7  14  16

4   6  13  20  22

10  12  19  21   3

11  18  25   2   9

(1) Programming ideas.

The odd order magic square matrix is constructed by the method of “upper right”

First, place 1 in the middle of the top row, and then place the succeeding numbers on the diagonal of the upper right in order, and make the following modifications:

1) When it reaches the top row, the next number is placed on the bottom row as if it were on top of the top row;

2) When the rightmost column is reached, the next number is placed in the leftmost column as if it is close to the right of the right column;

3) When the number has been filled in the position, or when the number reaches the upper right corner, the next number will be placed directly below the position just filled in.

Next, take the construction of a third-order magic square matrix as an example to illustrate the construction process of this method, as shown in Figure 1.

Figure 1   “ Constructing 3-order magic square matrix with “top right” continuous filling method

The program defines a two-dimensional array a [n] [n] to save the square matrix. Initially, all elements in the array are set to 0.

The variables row and col are used to store the position of the number num to be filled in the square matrix. Because the first number is placed in the middle of the top row, row row = 0, column col = n / 2 and number num = 1 are used initially.

The process of constructing a square matrix by using the “top right” continuous filling method is a cyclic program

While (Num < = n * n)

{

Determine the position row and col of num to be filled in;

Fill in num, that is, a [row] [col] = num;

num++；     // Next number to fill in

}

In the procedure, the method to determine the position to be filled in is as follows:

1) The succeeding numbers are placed on the diagonal line of the upper right slope in order, that is, row –;   col++；

2) There are three situations that need to be adjusted.

• When the top row is reached (row < 0), the,   row=n-1;
• When it reaches the rightmost column (Col = = n), col = 0;
• When the number has been filled in (a [row] [col]= 0)，   row+=2;      col–;

3) In one case, when the position in the upper right corner is reached (row = = 0 & & col = = n-1), special processing is performed directly, row++   。

(2) Source program.

#include

int main()

{

int t;

scanf(“%d”,&t);

while (t–)

{

int a[19][19],row,col,num,n;

scanf(“%d”,&n);

for (row=0;row

for (col=0;col

a[row][col]=0;

row=0;     col=n/2;    num=1;

a[row][col]=num;

while (num

{

num++;

if (row==0 && col==n-1)         // Get to the top right corner

row++;

else

{

row–;     col++;

if (row<0)  row=n-1;

if (col==n) col=0;

if (a[row][col]!=0)

{   row+=2;     col–;  }

}

a[row][col]=num;

}

for(row=0;row

{

for(col=0;col

printf(“%4d”,a[row][col]);

printf(“\n”);

}

}

return 0;

}

##### 29-2   Double even order magic square matrix

Problem description

When n is a double even number, that is, n = 4 * k, the steps of constructing the magic square matrix by using the bidirectional flip method are as follows:

(1) Fill the numbers 1 to n * N in the square matrix from left to right and from top to bottom.

(2) Flip all the numbers in half of the rows in the central part of the square matrix left and right.

(3) Flip all the numbers in half of the columns in the central part of the square matrix up and down.

Because of the need to flip twice in the process of construction, it is called bidirectional flip method. Next, take the construction of a 4-order magic square matrix as an example to illustrate the construction process of this method, as shown in Figure 2.

Figure 2   Construction of 4-order magic square matrix by bidirectional inversion method

Input format

It contains multiple groups of data. First, enter t to indicate that there are t groups of data. One row of each group of data shows that n (4 < = n < = 20) is a multiple of 4.

Output format

For each group of data, output n magic cube, each number occupies 4 spaces, right aligned

sample input

2

4

8

sample output

1  14  15   4

8  11  10   5

12   7   6   9

13   2   3  16

1   2  59  60  61  62   7   8

9  10  51  52  53  54  15  16

24  23  46  45  44  43  18  17

32  31  38  37  36  35  26  25

40  39  30  29  28  27  34  33

48  47  22  21  20  19  42  41

49  50  11  12  13  14  55  56

57  58   3   4   5   6  63  64

(1) Programming ideas.

In the program, a two-dimensional array a [n] [n] is defined to save the square matrix. When constructing, three double loops are carried out in turn.

1) Fill the numbers 1 to n * N in the square matrix from left to right and from top to bottom

num=1;

for (row=0; row

for (col=0; col

a[row][col] = num++;

2) Flip all the numbers in half of the rows in the central part of the square matrix left and right

For a double even matrix of order n = 4 * k, if it is divided into four groups by row, the range of row numbers of each group is 0 ~ k-1, K ~ 2k-1, 2K ~ 3K-1, 3K ~ 4k-1, and there are 2K rows in the middle. The row number of the middle row is K ~ 3K-1. Because k = n / 4, the row number of the middle row is n / 4 ~ n * 3 / 4-1.

For each row, flipping all the numbers in it to the left and right is actually to arrange a one-dimensional array in reverse order. Therefore, the operation of step 2 can be written as the following cycle:

for (row=n/4; row<=n*3/4-1; row++)

for (col=0; col

{

temp = a[row][col];

a[row][col] = a[row][n-1-col];

a[row][n-1-col] = temp;

}

3) Flip all the numbers in half of the columns in the central part of the square matrix up and down.

The operation class in step 3 is the same as that in step 2, except that the relationship between rows and columns is reversed, which can be written as the following loop:

for (col=n/4; col<=n*3/4-1; col++)

for (row=0; row

{

temp = a[row][col];

a[row][col] = a[n-1-row][col];

a[n-1-row][col] = temp;

}

(2) Source program.

#include

int main()

{

int t;

scanf(“%d”,&t);

while (t–)

{

int a[20][20],row,col,num,n,temp;

scanf(“%d”,&n);

num=1;

for (row=0; row

for (col=0; col

a[row][col] = num++;

for (row=n/4; row<=n*3/4-1; row++)

for (col=0; col

{

temp = a[row][col];

a[row][col] = a[row][n-1-col];

a[row][n-1-col] = temp;

}

for (col=n/4; col<=n*3/4-1; col++)

for (row=0; row

{

temp = a[row][col];

a[row][col] = a[n-1-row][col];

a[n-1-row][col] = temp;

}

for(row=0;row

{

for(col=0;col

printf(“%4d”,a[row][col]);

printf(“\n”);

}

}

return 0;

}

##### 29-3   Fractional matrix

Problem description

We define the matrix as follows:

1/1  1/2  1/3

1/2  1/1  1/2

1/3  1/2  1/1

The elements on the diagonal of the matrix are always 1 / 1, and the denominators of the fractions on both sides of the diagonal increase one by one.

Ask for the sum of this matrix.

Input format

Each row is given an integer n (n < 50000), which means that the matrix is n * n. when n is 0, the input ends.

Output format

Output the answer to 2 decimal places.

sample input

1

2

3

4

0

sample output

1.00

3.00

5.67

8.83

(1) Programming ideas.

The fractional matrix in the title is a symmetric matrix, so we can first calculate the sum of the lower triangular matrix. There are n 1 / 1 on the diagonal, so the sum of the diagonal is n, so the sum of the fractional matrix is 2 * sum-n.

There are n rows in the lower triangular matrix, and there are I numbers in the i-th row, which are 1 / I, 1 / (i-1),…, 1 / 2, and 1 / 1 in turn. Therefore, the calculation of the sum of the lower triangular matrix can be completed by a simple cycle.

(2) Source program.

#include

int main()

{

int n,i;

double presum,sum;

while (scanf(“%d”,&n) && n!=0)

{

presum=1.0;

sum=presum;

for (i=2;i<=n;i++)

{

presum+=1.0/i;       // Sum of each line 1 / I + 1 / (i-1) +… + 1 / 2 + 1 / 1

sum+=presum;       // The sum of rows in the lower triangular matrix

}

printf(“%.2lf\n”,sum*2-n);   // Sum of upper and lower triangular matrix minus sum of diagonal

}

return 0;

## Yiwen teaches you to quickly build a k8s cluster from zero

1. introduction K8s must be familiar to everyone. It can facilitate the centralized management and scheduling of docker containers, reduce the difficulty of operation and maintenance, and improve the quality of service. Let’s take a look at how to quickly build a k8s cluster from zero. 2. server hardware requirements Before deploying the k8s cluster, […]