【问题标题】:Printing Pascals Triangle (recursive) (JAVA)打印帕斯卡三角(递归)(JAVA)
【发布时间】:2014-11-05 04:04:56
【问题描述】:

到目前为止,我有这个,但我不太确定 printPTriangle 如何使用代码打印三角形。如果有人可以帮助我解决这个问题,我将不胜感激。

public static int factorial(int n) {
    if (n == 1) {
        return 1;
    }
    return n * (factorial(n - 1));
}

public static int pascalsNumber(int x, int y) {
    return factorial(x)/(factorial(y) * factorial((x - y))); //Using combinations formula
}

public static void printPTriangle(int z) {



}

【问题讨论】:

    标签: java recursion combinations pascals-triangle


    【解决方案1】:

    试试这个,

    public class PascalTriangle {
    
    public static void main(String[] args) {
    
        int rows = 10;
    
    
        for(int i =0;i<rows;i++) {
            int number = 1;
            System.out.format("%"+(rows-i)*2+"s","");
            for(int j=0;j<=i;j++) {
                 System.out.format("%4d",number);
                 number = number * (i - j) / (j + 1);
    
            }
            System.out.println();
        }
    
    }
    }
    

    注意上面用于创建格式良好的三角形的格式化命令。 %4d 指示格式化程序在 4 个空格内打印数字。

    【讨论】:

      【解决方案2】:

      我也是 Java 的初学者,正在研究 Pascals 三角形。我喜欢上面指出的格式并将其引入我的代码。我把间距调大了一点,以适应更大的三角形。

      导入 java.util.Scanner;

      public class Pascal {
      
          public static void main(String[] args) {
      
              Scanner scanner = new Scanner(System.in);
              System.out.print("Enter the row number up to which Pascal's triangle has to be printed: ");
              int row = scanner.nextInt();
              print(row);
      
              scanner.close();
          }
      
          public static void print(int n) {
              for (int i = 0; i < n; i++) {
                  System.out.format("%"+(n-i)*3+"s","");
                  for (int j = 0; j <= i; j++) {
                      System.out.format("%6d",(pascal(i, j)));
                  }
                  System.out.println();
              }
          }
      
          public static int pascal(int i, int j) {
              if (j == 0) {
                  return 1;
              } else if (j == i) {
                  return 1;
              } else {
                  return pascal(i - 1, j - 1) + pascal(i - 1, j);
              }
      
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        解决办法如下:

        public static void main(String[] args) 
        {
            pascal(1,10);
        }
        static int pascal(int start, int end)
        {
            if(start>=end)
                return 0;
            int number = 1;
            System.out.format("%"+(end-start)*2+"s","");
            pascal2(start,number,0);
            System.out.println();
            return pascal(start+1,end);
        }
        static int pascal2(int start,int number,int end)
        {
            if(end>start)
                return 1;
            System.out.format("%4d",number);
            return pascal2(start,number * (start - end) / (end + 1),end+1);
        }
        

        【讨论】:

          【解决方案4】:

          我只是被 Java 类的这个编程挑战难住了,在只使用没有循环的递归的任何地方都找不到帮助。因此,经过数小时的痛苦,它就在这里。它所需要的只是一个 main 方法或演示类来创建这个 PascalsTriangle 类的实例并使用行数对其进行初始化。

          public class PascalsTriangle {
          
          private StringBuilder str; // StringBuilder to display triangle
          
          /**
           * Starts the process of printing the Pascals Triangle
           * @param rows Number of rows to print
           */
          public PascalsTriangle(int rows) {
          
              str = new StringBuilder();
          
              printTriangle(rows, str);
          }
          
          /**
           * Uses recursion to function as an "outer loop" and calls
           * itself once for each row in triangle. Then displays the result
           * @param row The number of the row to generate
           * @param str StringBuilder to insert each row into
           */
          public static void printTriangle(int row, StringBuilder str) {
          
              // calls itself until row equals -1
              if (row >= 0) {
          
                  // calls lower function to generate row and inserts the result into front of StringBuilder
                  str.insert(0, getRow(row, 0) + "\n");
          
                  // calls itself with a decremented row number
                  printTriangle(row - 1, str);
              } else {
          
                  // when the base case is reached - display the result
                  JOptionPane.showMessageDialog(null, str);
                  System.exit(0);
              }
          }
          
          /**
           * Uses recursion to act as the "inner loop" and calculate each number in the given row
           * @param rowNumber Number of the row being generated
           * @param elementNumber Number of the element within the row (always starts with 0)
           * @return String containing full row of numbers or empty string when base case is reached
           */
          public static String getRow(int rowNumber, int elementNumber) {
          
              // calls itself until elementNumber is greater than rowNumber
              if (elementNumber <= rowNumber) {
          
                  // calculates element using combinations formula: n!/r!(n-r)!
                  int element = fact(rowNumber) / (fact(elementNumber) * (fact(rowNumber - elementNumber)));
          
                  // calls itself for each element in row and returns full String            
                  return element + " " + getRow(rowNumber, elementNumber + 1);
          
              } else return "";
          }
          
          /**
           * Helper function that uses recursion to calculate factorial of given integer
           * @param n Number to calculate factorial
           * @return Factorial
           */
          public static int fact(int n) {
              if (n <= 0)
                  return 1;
              else
                  return n * fact(n - 1);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多