【问题标题】:How do I make this into a Pascal's triangle instead of a Right triangle?如何将其变成帕斯卡三角形而不是直角三角形?
【发布时间】:2020-06-20 13:23:39
【问题描述】:

我需要一个代码来制作一个帕斯卡三角形。这段代码用于直角三角形,但我需要它是帕斯卡三角形。它需要有 10 行,并且在顶部和底部的中间确实有一个间隙。 谁能帮我解决这个问题? for 循环就好了。

public static int get_pascal(int row, int col) {
    if (col == 0 || col == row) {
        return 1;
    } else {
        return get_pascal(row - 1, col - 1) + get_pascal(row - 1, col);
    }
}

public static void main(String[] args) {
    //row size variable
    int rowNum = 5;

    levels = new String[rowNum];

    int i = 0;
    int arIndex = 0;
    System.out.println(recurseRow(i, rowNum, arIndex));
    System.out.println("                                 ");
    System.out.println(upsideDown(rowNum - 1));
}

//Recursion for row
public static String recurseRow(int i, int rowNum, int arrayIndex) {
    if (i == rowNum)
        return "";
    else {
        int k = 0;
        int next = i + 1;
        String str = recurseCol(i, k);
        levels[arrayIndex] = str;
        arrayIndex += 1;
        return str + "\n" + recurseRow(next, rowNum, arrayIndex);
    }
}

//Recursion for column
public static String recurseCol(int i, int k) {
    if (k > i)
        return "";
    else {
        int next = k + 1;
        return get_pascal(i, k) + " " + recurseCol(i, next);
    }
}

//upside down recursion
public static String upsideDown(int index) {
    if (index < 0) {
        return "";
    } else {
        String str = levels[index];
        index -= 1;
        return str + "\n" + upsideDown(index);
    }
}

【问题讨论】:

    标签: java arrays for-loop multidimensional-array pascals-triangle


    【解决方案1】:

    您可以在递归创建行的位置添加多个空格的前缀:而不是

    String str = recurseCol(i, k);
    

    你会有

    String str = "";
    for (int spaces = 0; spaces < 2 * (rowNum - i - 1); spaces++) { 
      str += " ";
    }
    str += recurseCol(i, k);
    

    您还需要将所有数字格式化为在 recurseCol 中具有相同的宽度,例如现在所有数字都是 3 位宽:

    return String.format("%3d %s", get_pascal(i, k), recurseCol(i, next));
    

    修改方法的结果代码:

    //Recursion for row
    public static String recurseRow(int i, int rowNum, int arrayIndex) {
        if( i == rowNum)
            return "";
        else {
            int k = 0;
            int next = i + 1;
      String str = "";
      for (int spaces = 0; spaces < 2 * (rowNum - i - 1); spaces++) {
          str += " ";
      }
            str += recurseCol(i, k);
            levels[arrayIndex] = str;
            arrayIndex += 1;
            return  str + "\n" + recurseRow(next, rowNum, arrayIndex);
        }
    }
    
    //Recursion for column
    public static String recurseCol(int i, int k) {
        if(k > i)
            return "";
        else {
            int next = k + 1;
            return String.format("%3d %s", get_pascal(i, k), recurseCol(i, next));
        }
    }
    

    “神奇”数字 2 和 3 是相互关联的:如果每个数字都是 n-digits 宽,那么我们需要在由重复 rowNum - i - 1 次的 n-1 空格组成的字符串中添加前缀。

    还有其他方法可以实现这些,但我认为以上方法可以通过最少的修改获得结果。

    【讨论】:

      【解决方案2】:

      帕斯卡三角形 - 是一个由二项式系数组成的三角形数组,其中第一行和第一列的元素都等于 1,所有其他元素都是行和列中的前一个元素。

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

      您可以创建一个迭代方法来填充这样的数组:

      public static int[][] pascalsTriangle(int n) {
          // an array of 'n' rows
          int[][] arr = new int[n][];
          // iterate over the rows of the array
          for (int i = 0; i < n; i++) {
              // a row of 'n-i' elements
              arr[i] = new int[n - i];
              // iterate over the elements of the row
              for (int j = 0; j < n - i; j++) {
                  if (i == 0 || j == 0) {
                      // elements of the first row
                      // and column are equal to one
                      arr[i][j] = 1;
                  } else {
                      // all other elements are the sum of the
                      // previous element in the row and column
                      arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
                  }
              }
          }
          return arr;
      }
      
      public static void main(String[] args) {
          int n = 10;
          System.out.println("n = " + n);
          System.out.println("Pascal's triangle:");
          int[][] arr = pascalsTriangle(n);
          for (int[] row : arr) {
              for (int element : row)
                  System.out.printf("%2d ", element);
              System.out.println();
          }
      }
      

      输出:

      n = 10
      Pascal's triangle:
       1  1  1  1  1  1  1  1  1  1 
       1  2  3  4  5  6  7  8  9 
       1  3  6 10 15 21 28 36 
       1  4 10 20 35 56 84 
       1  5 15 35 70 126 
       1  6 21 56 126 
       1  7 28 84 
       1  8 36 
       1  9 
       1 
      

      另见:Array of binomial coefficients

      【讨论】:

        猜你喜欢
        • 2012-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-12
        • 2015-01-29
        • 2011-05-07
        • 1970-01-01
        相关资源
        最近更新 更多