May 4, 2018

020 Realize the output of Yanghui triangle

1  public  class YangHuiTest
 2  {
 3      public  static  void main(String[] args)            // What does String[] args mean 
4       {
 5          int triangle[][]= new  int [10][];              // Create a two-dimensional array
 6                                                       // Traverse the two-dimensional array 
7          for ( int i = 0;i < triangle.length;i++ )
 8          {
 9              triangle[i] = new  int [i+1];              // initialize the second layer array size
 10                                                       / /Traverse the second level array 
11              for ( int j=0; j<=i;j++)                    // j<=i is written as j<=1 
12              {
 13                  if (i==0||j==0||j == i)
 14                  {
 15                      triangle[i][j]=1;                 // Assign both sides of the triangle to 1 
16                  }
 17                  else 
18                  {
 19                      triangle[i][j]=triangle[i-1][j] +triangle[i-1][j-1]; // Other cases: the number = the same column and front row + the front row and front row 
20                  }
 21                  System.out.print(triangle[i][j]+"\t" ) ;
22             }
23             System.out.println();
24         }
25         
26     }
27 
28 }

Remark:

1. What is the meaning of String[] args?

      

string[] args , is to declare a parameter array
In java, the parentheses of the main function have an array of parameters, and the call can take any number of parameters.
String indicates that the type of the parameter is a string type, [] indicates that the parameter is an array, the args number parameter array name
When using these parameters in the method, it can be achieved by accessing the array elements.
For example:
public static void main(String [] args) // can also be written as (String args[])
{
for(int i=0;i<args.length;i++)
System.out.println(args[0]) ;
}
When calling these parameters, the parameters are separated by spaces. If there is a space in the middle of a string, then use "" to cause the string.

     Note: This paragraph is excerpted from Baidu Zhidao

ice2921

2. Yanghui triangle characteristics

      1> Both sides of the triangle are 1

       2> Other situations:

                          triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];

                          Number = previous row in the same column + previous row in the previous column

3, triangle triangle (cover face)


 

Eclipse crashed last night, because the working space was too large and the memory stick and CPU could not bear it. My computer is i5, 4G, and it is still possible to run Eclipse (as long as I don't open the running space with many questions, it will not explode). The task to be completed yesterday was also delayed because of this incident. Yesterday, I almost gave up using Eclipse and switched to interllij idea, because I was too naive and gave up, and I will adjust it after a while. This morning, I re-installed Eclipse in the C drive (solid-state drive), and the effect is still good (but I will put too many Java projects in the file in the future, which will affect the operation of the computer)


 

How to represent 021 hollow rhombus

 1 public class Diamond 
 2 {
 3     public static void main(String[] args)
 4     {        printHollowRhombus(6);
 5      }
 6     public static void printHollowRhombus(int size)
 7     {
 8         if(size % 2==0)
 9         {
10             size++;                           //计算菱形大小
11         }
12         for(int i=0;i<size/2+1;i++)
13          {
 14              for ( int j = size/2+1;j>i+1;j-- )
 15              {
 16                  System.out.print(" ");      // Output the blank at the upper left corner 
17              }
 18              for ( int j=0;j<=2*i+1;j++ )
 19              {
 20                  if (j==0||j==2* i)
 21                   {
 22                      System.out.print("* ");    // output diamond upper half edge 
23                   }
 24                  else 
25                  {
26                      System.out.print(" ");    // Output the hollow upper half of the diamond 
27                  }
 28              }
 29              System.out.println("");            // Line feed      
30          }
 31          for ( int i =size/2+1 ;i<size;i++ )
 32          {
 33              for ( int j=0;j<i-size/2;j++ )
 34              {
 35                  System.out.print(" ");        // Output the rhombus seating corner blank 
36              }
 37              for (int j=0;j<2*size-1-2*i;j++ )
 38              {
 39                  if (j==0||j==2*(size-i-1 ))
 40                  {
 41                      System.out. print("* ");    // Output the edge of the lower half of the diamond 
42                  }
 43                  else 
44                  {
 45                      System.out.print(" ");    // Output the hollow of the lower half of the diamond 
46                  }
 47              }
 48              System.out.println ("");            // line feed 
49          }
 50      }
51 
52 }

operation result:

Remarks: This question takes a lot of time (the problem lies in: the use of the for statement in the program, the designer is perfect. So I want to have a deeper understanding of the smooth process of each step. Because it is not written by myself in terms of ideas It is very understandable, during which the Debug function is used).

First, the implementation steps of the Debug function: 1. First determine the breakpoint (double-click the number of lines on the left side of the code, and then a small dot will appear.), there is a bug-like icon at the top of the menu. After clicking, the program will run to The small circle stops, press F6 to run step by step. Then exit the debug mode: you will see a small bug in the upper right corner of the screen and click to exit. (This took a long time, but still didn't solve my problem)

Second, the idea of ​​the program: 1. The premise is that the mathematical operation idea has been constructed. 2. Control the output of blank spaces and characters through a double-layer for loop. 3. First divide the rhombus into upper and lower parts, and then divide the upper and lower parts into left and right parts: the short blank part in the front and the characters and blank parts in the latter paragraph;

(core part)

for(int i=0;i<size/2+1;i++)            //calculate the number of lines to output

for (int j = size/2+1;j>i+1;j--) //Control the number of blank cells in each line (from more to less)
for(int j=0;j<=2*i+1 ;j++) //Determine whether to output a blank grid or a character
if(j==0||j==2*i) //Determine whether to output a blank grid or a character

else System.out.println("") //Line transfer
for(int i =size/2+1;i<size;i++) //Calculate the number of lines to output
for(int j=0;j<i-size/2;j++) //Control each line Number of blank cells (from less to more)
for(int j=0;j<2*size-1-2*i;j++) //Determine whether to output blank cells or characters
if(j==0||j ==2*(size-i-1)) //Determine whether to output a blank grid or a character
else System.out.println("") //Turn line

022 Print the nine-nine multiplication table

 1 package jiujiuTable;
 2 
 3 public class jiujiuTable 
 4 {
 5 
 6     public static void main(String[] args)
 7     {
 8         for(int i=1;i<=9;i++)
 9         {
10             for(int j=1;j<=i;j++)
11             {
12                 System.out.print(j+"*"+i+"="+i*j+"\t");
13             }
14             System.out.println("");
15         }
16     }
17 }

Remarks: The selected item cannot be started, and any startup has been performed recently. Reason: main is written as mian and cannot be calculated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325302444&siteId=291194637