[Monday] Pascal's Triangle count (Pascal triangle)

Pascal's Triangle, is a binomial coefficients A triangle geometry, China Southern Song Dynasty mathematician Pascal's 1261 book " Detailed IX algorithm ", a book appeared. In Europe, Pascal found that in 1654 (1623 ---- 1662) law , so this table is also called Pascal's triangle . Pascal found than Pascal as late as 393 years, than Jia Xian later than 600 years.

characteristic:

  1. Each number is equal to the sum of the two numbers above it.
  2. Each row of numbers symmetrically by 1 is gradually increased.
  3. Digital line n has n items.
  4. Number m of n-th row may be represented as  C (n-1, m-1) , that is, taking the number of combinations m-1 elements different from the n-1 elements.
  5. The number m of the n-th row and the n-m + 1 number equal to the number of combinations one of the properties.
  6. Each digit equal to the previous line and about two numbers. This property can be used to write the entire Pascal's Triangle. I.e. the n + 1 i-th row is equal to the number of i-1 and i-th and the n-th row sum, one of which is a combination of several properties. I.e.,  C (n-+. 1, I) = C (n-, I) + C (n-,-I. 1) .
  7. (a + b) of the expandable n coefficient sequence corresponding to the first (n + 1) Pascal's triangle in each row.
  8. The first number 2n + 1 of the first row, the second with 2n + 2 number of the third row, the row number of 2n + 3 ...... 5 in a line, and the first of these numbers 4n + 1 th Fibonacci lease number ; the first row of the second number 2n (n> 1), with the first 2n-1 number of row 4, line number 2n-2 6 ...... sum of these first and 4n-2 th Fei the number of waves that deed.
  9. The numerical values ​​of the n-th line, are multiplied by the number of columns 10 ^ m-1, then the sum of these values ​​is equal to 11 and the n-1 power. Examples: The first 11 rows are 1,10,45,120,210,252,210,120,45,10,1, then 1 * 11 ^ 10 ^ 10 = 0 + 1 + 10 * 45 * 10 ^ 10 ^ 2 + ... + 1 * 10 ^ 10 = 25937424601

Problem-solving, you can use the first 6:00 characteristic write Pascal's Triangle.

Code:

package com.jandmin.demo.leetcode;

/**
 * @description: 杨辉三角
 *               杨辉三角,是二项式系数在三角形中的一种几何排列。在欧洲,这个表叫做帕斯卡三角形。
 *               帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年,比贾宪迟600年。
 *               杨辉三角是中国古代数学的杰出研究成果之一,它把二项式系数图形化,
 *               把组合数内在的一些代数性质直观地从图形中体现出来,是一种离散型的数与形的结合
 *               1
 *             1  1
 *           1  2  1
 *          1  3  3  1
 *         1  4  6  4  1
 *        1  5 10 10  5  1
 * @author: JandMin
 * @create: 2019-06-26 12:02
 **/
public class YangHuiTriangle {
    /**
     * 方案一循环次数
     */
    private static int count1;
    /**
     * 方案二循环次数
     */
    private static int count2;
    public static void main(String[] args) {
        int n = 10;
        long start = System.currentTimeMillis();
        int[][] triangle1 = getTriangle1(n);
        System.out.println("方案一:count1 = " + count1 + " 耗时 " + (System.currentTimeMillis() - start) + " ms");
        print(triangle1);
        start = System.currentTimeMillis();
        int[][] triangle2 = getTriangle2(n);
        System.out.println("方案二:count2 = " + count2 + " 耗时 " + (System.currentTimeMillis() - start) + " ms");
        print(triangle2);
    }
    /**
     * @Description: 方案一:获取前n行杨辉三角
     * @Date: 2019/6/26
     * @param n
     * @return: int[][]
     */
    private static int[][] getTriangle1(int n) {
        if (n <= 0){
            return null;
        }
        int[][] array = new int[n][n];
        for(int i=0; i<n; i++){
            int[] row = getRow(i+1);
            array[i] = row;
        }
        return array;
    }

    /**
     * @Description: 获取第n行数组
     * @Date: 2019/6/26
     * @param n
     * @return: int[]
     */
    private static int[] getRow(int n) {
        if (n <= 0){
            return new int[0];
        }
        int[] array = new int[n];
        array[0] = 1;
        array[n-1] = 1;
        if(n <= 2){
            return array;
        }
        for (int i=1; i<n-1; i++){
            count1++;
            int[] before = getRow(n-1);
            array[i] = before[i] + before[i-1];
        }
        return array;
    }
    /**
     * @Description: 方案二:获取前n行杨辉三角
     * @Date: 2019/6/26
     * @param n
     * @return: int[][]
     */
    private static int[][] getTriangle2(int n) {
        if (n <= 0){
            return null;
        }
        int[][] array = new int[n][];
        int[] before = null;
        for(int i=0; i<n; i++){
            int[] row = getRow(i+1,before);
            before = row;
            array[i] = row;
        }
        return array;
    }
    /**
     * @Description: 获取第n行数组
     * @Date: 2019/6/26
     * @param n
     * @param before  前一行数组
     * @return: int[]
     */
    private static int[] getRow(int n,int[] before) {
        int[] array = new int[n];
        array[0] = 1;
        array[n-1] = 1;
        if(n <= 2){
            return array;
        }
        for (int i=1; i<n-1; i++){
            count2++;
            array[i] = before[i] + before[i-1];
        }
        return array;
    }
    /**
     * @Description: 打印杨辉三角
     * @Date: 2019/6/26
     * @param triangle
     * @return: void
     */
    private static void print(int[][] triangle){
        int n = triangle.length;
        for(int i=0; i<n; i++){
            for (int s=n-i; s>0; s--){
                System.out.print(" ");
            }
            int[] row = triangle[i];
            int len = row.length;
            for(int j=0; j<len; j++){
                System.out.print(row[j]);
                if(row[j]<10) {
                    System.out.print("   ");
                } else if(row[j]<100){
                    System.out.print("  ");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Print Results:

Guess you like

Origin blog.csdn.net/ItRuler/article/details/93760490
Recommended