Example 28 serpentine linear array
Problem Description
Programming, the natural numbers 1,2, ..., N 2 in a serpentine manner into N-by-order square matrix. For example, when N = 3 and N = 4 linear serpentine array shown in Figure 1 below.
FIG serpentine linear array 1
Input Format
A positive integer n (1≤n≤20).
Output Format
To meet the requirements of order N linear serpentine square. Total output n lines, each line number n, representing the number of four each.
SAMPLE INPUT
4
Sample Output
13 14 15 16
12 11 10 9
5 6 7 8
4 3 2 1
(1) programming ideas.
As can be seen from Figure 1, the serpentine configuration of the linear array from the bottom row (row = N-1) to the top row (row = 0) is performed. Fill each row between the two switch ways, one way is to order from right to left (i.e., for (j = n-1; j> = 0; j--)) is incremented by one sequentially fill, referred to Embodiment 1; another way is to order from left to right (i.e., for (j = 0; j <= n-1; j ++)) is incremented by one sequentially fill, called mode 2.
A variable k is defined in the program to mark these two methods, the initial value k 0, 1 represents by way of the current fill row manner after 1, changing the value of k to be equal to 1, 2 represents by way of the current row after 2 fill manner, then changing the value of k to be equal to 0.
(2) source.
#include<stdio.h>
int main ()
{
int a[20][20]={0},n;
scanf("%d",&n);
int i,j,k=0,t=1;
for (i = n-1; i> = 0; i--) // traverse line
{
if (k == 0) // increment the order from right to left memory array element 1
{
for(j=n-1;j>=0;j--)
a[i][j]=t++;
k=1;
}
else // sequentially in ascending order from left to right memory array element 1
{
for(j=0;j<=n-1;j++)
a[i][j]=t++;
k=0;
}
}
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
return 0;
}
Exercise 28
28-1 slash serpentine array
Problem Description
Programming, the natural numbers 1,2, ..., N 2 in a serpentine manner into N-by-order square matrix. For example, when N = 3 and N = 4 matrix shown in FIG. 2 as follows.
FIG hatched serpentine array 2
Input Format
A positive integer n (1≤n≤20).
Output Format
N serpentine order to meet the requirements shaded square. Total output n lines, each line number n, representing the number of four each.
SAMPLE INPUT
5
Sample Output
15 16 22 23 25
7 14 17 21 24
6 8 13 18 20
2 5 9 12 19
1 3 4 10 11
(1) programming ideas.
Hatched serpentine array shown in FIG observation shows that the number of square-by during the filling configuration, is carried along The oblique A is inclined, one is oblique, as shown in FIG. 3 ( ) as shown in a.
The line number currently set position to enter the number of row (row between 0 ~ n-1), the column number col (col also between 0 ~ n-1). When the fill according to the oblique position of the next row ++, col ++; if filled by an oblique direction, then the next location row -, col--. Since the next position may be beyond the boundaries of the square, so sometimes we need to be adjusted. There are four cases adjustments, are described separately below.
3 a schematic configuration of a serpentine array of oblique lines in FIG.
When diagonally downward to fill in, two things occur:
1) than the position of the bottom row (i.e., row == n), FIG. 3 (b), the rear fill good 3 calculates a position of the lower bounds of the number 4, this time adjustment process is not the column col variable, row minus 1 (i.e. row--).
2) exceeds the rightmost column position (i.e., col == n), FIG. 3 (c), the rear well 15 filled, calculating a position of the lower bounds of the number 16, this time adjustment process is col-- , row = row-2.
A special case, to fill the well 10, to calculate the next position of number 11, the rows and columns are out of line, but with the column cross-border processing method, and therefore should be processed in the program where col == n, and then the processing row == n Case. Thus for this special case, since the process after the col == n, row minus 2, without crossing, and therefore not be processed in the case where row == n.
When filling out the oblique, there will be two situations:
1) than the position of the first row (i.e., row == - 1), FIG. 3 (d), the rear well 13 filled, calculating a position of the lower bounds of the number 14, this time adjustment process to row ++, col = col-2.
2) than the position of the leftmost column (i.e., col == - 1), FIG. 3 (e), the rear 6 well filled, calculating a position of the lower number of cross-border 7, this time adjustment process is not row variable column number plus 1 (i.e., col ++).
A special case, the third-order square matrix as shown in (f) of FIG. 3, to fill the well 6, to calculate the next position number 7, both rows and columns of cross-border, which bounds the peer processing method, thus the program It should handle row == - 1, reprocessing col == - 1 case. Thus for this special case, since the processing of the row == - after 1, 2 COL added, without crossing, and therefore not be processed col == - 1 case.
After adjusting the number of cross-border fill each time, the number of fill direction will change. Therefore, a variable can be set up, when up = 1, represents the number of refills obliquely; when up = 0, represents the number of refills obliquely downward.
When initialized, make up = 1, row = n-1, col = 0, num = 1; 1 fill the current position (i.e., a [row] [col] = 1), then circulated until the number n * n number completed. Cycle, always press the up direction, to determine the next position, and then fill in the corresponding number. For example, 1 up to 2, out of range, can be adjusted, FIG. 3 (e) in FIG.
(2) source.
#include<stdio.h>
int main ()
{
int a[20][20]={0},n;
scanf("%d",&n);
int up=1;
int row=n-1;
int col=0;
int num = 1;
a[row][col]=num++;
while (num<=n*n)
{
if (up) { row--; col--;}
else { row++; col++;}
if (row == - 1) // more than the first line of position
{ row++; col=col+2; up=1-up; }
if (col == n) // beyond the position of the rightmost column
{ row=row-2; col--; up=1-up; }
if (row == n) // beyond the position of the bottom row
{ row--; up=1-up; }
if (col == - 1) // position than the leftmost column
{ col++; up=1-up; }
a[row][col]=num++;
}
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
return 0;
}
If the initial value of up procedure is set to 0, i.e. obliquely downward begins to fill. It recompiling and executing the above procedures, to give the results shown below.
5
11 19 20 24 25
10 12 18 21 23
4 9 13 17 22
3 5 8 14 16
1 2 6 7 15
28-2 symmetrical square
Problem Description
7 is shown in FIG. 4 two symmetrical square order, image clarity, may be (a) matrix called circularly symmetrical square, (b) is called a square matrix symmetric triangular matrix.
FIG 4 symmetric square matrix
Input Format
Two positive integers n (1≤n≤20), and k (k is 1 or 2).
Output Format
N square matrix symmetrical order to meet the requirements. If k = 1, the output circularly symmetrical square, k = 2, the output of symmetrical triangular matrix. Total output n lines, each line number n, each representing the number of three.
Sample input 1
5 1
Sample Output 1
0 1 1 1 0
1 0 2 0 1
1 2 0 2 1
1 0 2 0 1
0 1 1 1 0
Sample input 2
7 2
Sample Output 2
0 1 2 3 2 1 0
1 0 1 2 1 0 1
2 1 0 1 0 1 2
3 2 1 0 1 2 3
2 1 0 1 0 1 2
1 0 1 2 1 0 1
0 1 2 3 2 1 0
(1)编程思路。
1)生成如图4(a)所示的环形对称方阵的方法。
设方阵中元素的行号为row,列号为col。为方便见,row和col均从1开始计。
方阵的主对角线(即row==col)和次对角线(即row+col==n+1)的各元素均赋值“0”。
按两条对角线把方阵可分成上部、左部、右部与下部4个区,如图5所示。
图5 环形对称方阵的四个分区
四个分区的赋值方式为:
上部按行号row赋值,即if (row+col<n+1 && row<col) a[row][col]=row。
下部按表达式n+1-row赋值,即if (row+col>n+1 && row>col) a[row][col]=n+1-row。
左部按列号col赋值,即if (row+col<n+1 && row>col) a[row][col]=col。
右部按表达式n+1-col赋值,即if (row+col>n+1 && row<col) a[row][col]=n+1-col。
2)生成如图4(b)所示的三角形对称方阵的方法。
令m=(n+1)/2,按图6(a)所示分成4个区。
图6 三角形对称方阵的四个分区
仔细分析这个四个分区的元素值与行号、列号的关系,并参照图6(b)所示的7阶对称方阵各元素的值,可归纳出:
左上区(row<=m && col<=m)与右下区(row>m && col>m)参照主对角线赋值:
a[row][col]=abs(row-col) 。
右上区((row<=m && col>m)与左下区(row>m && col<=m)参照次对角线赋值:
a[row][col]= abs(row+col-n-1)。
(2)源程序。
#include <stdio.h>
#include <math.h>
int main()
{
int n,k,row,col,a[21][21]={0};
scanf("%d%d",&n,&k);
if (k==1)
{
for (row=1; row<=n; row++)
for (col=1; col<=n; col++)
{
if (row==col || row+col==n+1)
a[row][col]=0; // 方阵对角线元素赋值
if (row+col<n+1 && row<col)
a[row][col]=row; // 方阵上部元素赋值
if (row+col<n+1 && row>col)
a[row][col]=col; // 方阵左部元素赋值
if (row+col>n+1 && row>col)
a[row][col]=n+1-row; // 方阵下部元素赋值
if (row+col>n+1 && row<col)
a[row][col]=n+1-col; // 方阵右部元素赋值
}
}
else
{
int m=(n+1)/2;
for (row=1; row<=n; row++)
for (col=1; col<=n; col++)
{
if ((row<=m && col<=m) || (row>m && col>m))
a[row][col]=abs(row-col); // 方阵左上部与右下部元素赋值
if ((row<=m && col>m) || (row>m && col<=m))
a[row][col]=abs(row+col-n-1); // 方阵右上部与左下部元素赋值
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
printf("%3d",a[i][j]);
printf("\n");
}
return 0;
}
28-3 螺旋下三角阵
问题描述
编写程序,将自然数1、2、…、(1+N)*N/2按螺旋方式逐个顺序存入N阶下三角矩阵。例如,当N=3和N=4时的矩阵如下图7所示。
图7 螺旋下三角阵
输入格式
一个正整数n(1≤n≤20)。
输出格式
N阶满足要求的螺旋下三角阵。输出时每个数占4列。
输入样例
5
输出样例
1 2 3 4 5
12 13 14 6
11 15 7
10 8
9
(1)编程思路
螺旋下三角阵的构造可以看成由向右填充(行号不变、列号加1,即col++)、斜向下填充(row++、col--)和向上填充(行号减1、列号不变,即row--)三个子过程不断交替完成的。
例如,图8所示的3阶螺旋下三角阵可以看成由向右填充(1、2、3),斜向下填充(4、5)和向上填充(6)这3个子过程完成的。4阶螺旋下三角阵可以看成由向右填充(1、2、3、4),斜向下填充(5、6、7)、向上填充(8、9)和向右填充(10)这4个子过程完成的。
n阶螺旋下三角阵可以看成由n个子过程完成,每个子过程为向右填充、斜向下填充和向上填充这三种中的一种,用变量direction来表示,其取值为0、1或2,0表示向右填充,1表示斜向下填充,2表示向上填充。每个子过程结束后,切换填充方向,方式为:
direction=(direction+1)%3;
n个子过程中,第1个子过程填写n个数,第2个子过程填写n-1个数,…,最后一个子过程填写1个数。因此,程序总体写成一个二重循环,描述为:
for (i=n;i>=1;i--)
{
for (j=1;j<=i;j++)
{
按填充方向,填充相应数据
}
direction=(direction+1)%3; // 切换填充方向
}
初始时,注意row=0,col=-1,这样向右col++后,col为0,正好填在第1个位置。
(2)源程序。
#include <stdio.h>
int main()
{
int a[20][20]={0},row,col,i,j,n,num;
int direction=0;
scanf("%d",&n);
row=0; col=-1; num=1;
for (i=n;i>=1;i--)
{
for (j=1;j<=i;j++)
{
switch(direction)
{
case 0:col++;break; // 向右填充
case 1:row++;col--;break; // 斜向下填充
case 2:row--;break; // 向上填充
}
a[row][col]=num++;
}
direction=(direction+1)%3; // 切换填充方向
}
for(row=0;row<n;row++)
{
for(col=0;col<n-row;col++)
printf("%4d",a[row][col]);
printf("\n");
}
return 0;
}