Using the number of columns output symmetrical diamond pattern, Oh

      Today, in retrospect JavaSE, once they saw a small example of the practice, with a diamond output [*]. Figure

  *  
 *** 
*****
 *** 
  *  

                         figure 1

According to their previous logic will choose half of the half of the output, as above, the output of a first cone, following a re-output cone. If data to be output, then a matrix, FIG. 2, but also to select the output [@], [*] and then output, and then outputs [@].

Here is a convenient display, to replace the space became [@].

@@@*@@@
@@***@@
@*****@
*******
@*****@
@@***@@
@@@*@@@

                         FIG 2
code is as follows:

 1 public static void test2(int rowC) {
 2         int i,j,k,l;
 3         int midNum = rowC/2 + 1;
 4         for(i=1;i<=midNum;i++){
 5             
 6             for(j=1;j<=midNum-i;j++)
 7                 System.out.print("@");
 8 
 9             for(k=1;k<=2*i-1;k++)
10                 System.out.print("*");
11             
12             for(l=1;l<=rowC-j-k+2;l++)
13                 System.out.print("@");
14 
15             System.out.print("\n");
16 
17         }
18 
19         for(i=1;i<=midNum-1;i++){
20             
21             for(j=1;j<=i;j++)
22                 System.out.print("@");
23 
24             for(k=1;k<=rowC-2*i;k++)
25     
26                 System.out.print("*");
27 
28             for(l=1;l<=rowC-j-k+2;l++)
29                 System.out.print("@");
30             
31             System.out.println();
32 
33         }
34     }

The idea is to go first algorithm middle row, then the output from the first row to the middle row. This process does, there are sequentially output of [@], [*], [@].
When the lower half of the final output, too. There are not found, looking very simple, that is when the trouble code, and repeated too much, when you want to change the character output, big changes. Ever since, bloggers wanted to have an algorithm can not, as little code to implement it.

Ever since, he began to study the relationship between the output line with the number of characters in the output (the number of columns is like study the same thing).

At first I wanted to cycle in a row, put [@] [*] All output into the desired form such as @@@ @@@ *.

The first step, identify each line of output [*] number 1,3,5,7,5,3,1. This symmetrical form combination data.

The second step, to find out each line of output [*] start and end positions when.

             start end   

          Line 1: 44

          Line 2: 35

          Line 3: 26

          Line 4: 17

          Line 5: 26

          Line 6: 35

          Line 7: 44

      There are not found in the form of a symmetrical series.

The third step is to find out the relationship between the number of rows and columns of output above.

             Start End [*] The number of

          Line 1: 441

          Line 2: 353

          Line 3: 265

          Line 4: 177

          Line 5: 265

          Line 6: 353

          Line 7: 441

    First, we found that ( 1) the end - start + 1 = [*] The number of

                                 (2) Number of rows - 2 * | intermediate column number - current line | [*] = Number (ps: Since 7 rows 7, so that the middle column number is 4. | | representative of absolute value)

         (3) Start = | intermediate column number - current line | + 1

After three equations summarize the above, I feel I can row equation solved.

Assumptions: start x, ends of Y, (the main purpose is to start and end positions is calculated, then, in a column cycle can output the final result)

   Current behavior j, the number of rows is A, the middle column number midNum

    【1】 y - x + 1= A - 2 * | midNum - j | 

         【2】 x = | midNum - j | + 1

     [2] then substituted into the formula [1] is, y = A - | midNum - j |

     We know the value of midNum and A, respectively, 4 and 7, the x, y values ​​will be used to determine the current line number. It can be programmed. Ha ha.

code show as below:

public static void test1(int rowC) {
        
        int midNum = rowC/2 + 1;
        
        for(int j = 1; j <= rowC; j++) {
            
            
            for(int i = 1; i <= rowC; i++) {

                if(i >= (Math.abs(midNum-j) + 1) && (i <= (rowC-Math.abs(midNum-j)))) {
                    System.out.print("*");
                }else {
                    System.out.print("@");
                }
            }
            System.out.println();
        }
    }

I am not feeling a lot less than the first code. In fact, the knowledge of which use symmetric number sequence (ps: I have also done research online symmetrical Sequence with Baidu became clear, it is its own projections of general term formula).

As shown below, each row number of the output [*]: 1,3,5,7,5,3,1 is a Symmetric arithmetic arithmetic increments of 2 series. The term formulas n is the current line number, item symmetry C, A is the total number of rows with the same value. the subscript k is an intermediate term (A / 2 + 1) or (A + 1) / 2.


            

Guess you like

Origin www.cnblogs.com/leafIcesun/p/11456363.html