When learning ANSI C, PASCAL, JAVA and other programming languages, some graphics with "symmetric" properties are often encountered, requiring the knowledge of the knowledge to be used to reproduce these graphics. Most people will think of how to find out graphics through mathematical knowledge, and then solve problems. However, it is often looking for a law, and it is quite a worker. When studying ANSI C, encounter a question shown in right, requiring printing of this form of N * N:
At that time, the students were looking for rules for the brain juice. In the last 1 1 1 1, the best solution, but the best one is a three-story nest of 1 2 2 2 1 for statement. Using coverage methods to solve. After 1 2 3 2 1, I am thinking that there is a simpler algorithm? Suddenly, I found that 3 exactly the axis of 5 * 5 square arrays 1 1 1 1 1 symmetric center. If it makes an XOY plane vertical coordinate, the coordinate analysis of each integer point is found, and their absolute values of their horizontal coordinates are added with the respective absolute values of the vertical coordinates, just It is 2. Why don't you use absolute value functions ABS () or FABS ()? So I define two integer variables I and J, which are used to locate the horizontal, vertical coordinates of the integer point. The initial value is -2, the step size is 1, the control conditions are 2, and 2 for statements Nest. When ABS (I) <= ABS (J) is satisfied, the following loop: Printf ("% 3d", 3-ABS (i)); otherwise, Printf ("% 3d", 3-ABS (j)). I didn't expect to debug in the Windows98 Turbo C environment, pass! Ok, this has stimulated my idea, can I give this general algorithm? Thinking, first must satisfy the integer N is an odd number, then see if the loop control conditions meet ABS (i) <= ABS (J), which is different from different outputs. In the Turbo C environment, debugging, wondering for a while, and passed. Now give this general algorithm as follows: #include
Void Func (INT N) / * N Must Be An Odd Number And N Must Be Greater Than Zero * / {INT I, J, TEMP; TEMP = N / 2 1; For (i = -N / 2; i < = N / 2; i ) {for (j = -n / 2; j <= n / 2; j ) IF (ABS (i)> = ABS (j))) Printf ("% 3D", TEMP-ABS ( I)); Else Printf ("% 3D", TEMP-ABS (J)); Printf ("/ n");}}