[Java self-study notes # #] the tenth day of basic Java programming structure

Textbook: "Java core technology Volume"

The basic program structure design of Java Chapter III

3.10.8 irregular array

Exercises in the book there is a number k is drawn from inside the number n, calculate your probability of winning.

In general, if the number k extracted from the n numbers, there will be:

n × (n-1) × (n-2) × (n-3) × ...... × (n-k + 1) / 1 × 2 × 3 × ...... × k species possibility.

Since k is always not larger than n, so all of these possibilities in the array, it would be a triangular array, as follows:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1 

1, according to a conventional solution sleeve formula as follows:

package array;

public class IrregularArray 
{
	public static void main(String[] args)
	{
		final int NMAX = 10;
		int [] [] odds = new int [NMAX + 1] []; // create a two-dimensional array
		for (int n=0;n<=NMAX;n++)
		{
			odds [n] = new int [n + 1]; // triangular frame structures
		}
		
		for(int n=0;n<odds.length;n++)
		{
			for(int k=0;k<odds[n].length;k++)
			{
				int lotterOdds=1;
				for(int i=1;i<=k;i++)
				{
					lotterOdds = lotterOdds*(n-i+1)/i;
					odds [n] [k] = lotterOdds; // data array into one
				}
				
			}
		}
		for(int[] row:odds)
		{
			for(int odd:row)
				System.out.printf ( "% 4d", odd); // iterate all the elements of the output array
			System.out.println();            
		}
	}

}

  The results were as follows:

   0
   0   1
   0   2   1
   0   3   3   1
   0   4   6   4   1
   0   5  10  10   5   1
   0   6  15  20  15   6   1
   0   7  21  35  35  21   7   1
   0   8  28  56  70  56  28   8   1
   0   9  36  84 126 126  84  36   9   1
   0  10  45 120 210 252 210 120  45  10   1

2, there is a more tricky, no method of solving equation sets:

Observation triangular array of topics given in advance, the array elements that can satisfy a rule:

When the row index (i) and column index (j) are greater than 0, numbers [i] [j] = numbers [i-1] [j-1] + numbers [i-1] [j]. // Suppose Numbers [] [] array is triangular in question

So the code can also write:

package array;

public  class IrregularArray
{
    public static void main(String[] args)
    {
        Final  int ROWS =. 7 ;
         Final  int colums =. 7 ;
         int [] [] = trianglearrays new new  int [ROWS] [colums]; // create a two-dimensional array of size has been given
         for ( int R & lt = 0; R & lt <ROWS; ++ r )
        {
            trianglearrays [R & lt] [ 0] =. 1 ; // the elements of the first row of the array of charged
        }
        for (int c=0;c<COLUMS;c++)
        {
            trianglearrays [C] [C] =. 1 ; // index equal to the ranks of elements of the array loaded
        }
        for(int c1=1;c1<trianglearrays[0].length;c1++)
        {
            for (int r1=1;r1<trianglearrays.length;r1++)
            {
                if(c1!=r1)
                {
                    trianglearrays [R1] [C1] = trianglearrays [R1-1] [C1-1] + trianglearrays [R1-1 ] [C1]; // set of mathematical laws charged with other elements
                }
            }
        }
        
        for (int [] row:trianglearrays)
        {
            for (int b:row)
                System.out.printf ( "% 4D" , B); // traverse the array element output
            System.out.println();
        }
        
    }

}

The results were as follows:

1 0 0 0 0 0 0
1 1 0 0 0 0 0
1 2 1 0 0 0 0
1 3 3 1 0 0 0
1 4 6 4 1 0 0
1 5 10 10 5 1 0
1 6 15 20 15 6 1

3, ignoring the system automatically filled in the space 0, then two kinds of programming are desirable. The first way, the realization set up a triangular frame approach really new. This also shows that: write code does not just do the math problems the law, but also with coding tools, data structures, and other features, the code seeks to show the best results.

 

Guess you like

Origin www.cnblogs.com/yizhinailu/p/12465258.html