【问题标题】:Pascal's triangle positioning帕斯卡三角定位
【发布时间】:2013-09-22 00:00:53
【问题描述】:

我编写了一个打印出帕斯卡三角形的 Java 程序,但是我不知道如何正确定位它。

计划 1

public class Triangle {
    public static void main() {
        System.out.println("\nTriangle: ");
        int row = 11;
        long[][] triangle = new long[row][row];
        triangle[1][1] = 1;
        System.out.print(triangle[1][1] + "\n");

        for (int i = 2; i < row; i++) {
            for (int n = 1; n < row; n++) {
                triangle[i][n] = triangle[i-1][n-1] + triangle[i-1][n];
                if (triangle[i][n] > 0) {
                    System.out.print(triangle[i][n] + " ");
                }
            }
            System.out.println();
        }
    }
}

输出:

1
1 1 
1 2 1 
1 3 3 1 

方案 2

public class Triangle {
    public static void main() {
        System.out.println("\nTriangle: ");
        int row = 11;
        long[][] triangle = new long[row][row];
        int x = 1;
        while (x < row - 1) {
            System.out.print(" ");
            x++;
        }
        triangle[1][1] = 1;
        System.out.print(triangle[1][1] + "\n");

        for (int i = 2; i < row; i++) {
            x = i;
            while (x < row - 1) {
                System.out.print(" ");
                x++;
            }
            for (int n = 1; n < row; n++) {
                triangle[i][n] = triangle[i-1][n-1] + triangle[i-1][n];
                if (triangle[i][n] > 0) {
                    System.out.print(triangle[i][n] + " ");
                }
            }
            System.out.println();
        }
    }
}

输出:

     1
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 
1 5 10 10 5 1 //(Notice this line is incorrectly positioned)

当三角形接近多个数字时,它开始分解并使其变得丑陋。有人能解释一下如何显示一个普通的三角形而不是这个丑陋的三角形吗?

【问题讨论】:

标签: java arrays multidimensional-array pascals-triangle


【解决方案1】:

动态帕斯卡三角发生器在这里:

import java.io.IOException;
import java.util.Scanner;

public class Main {
    static double fact(int n) {
        double result = 1;
        for (double i = 1; i <= n; i++)
            result *= i;
        return result;
    }

    static double combine(int n, int r) {
        return ((fact(n)) / (fact(n - r) * fact(r)));
    }

    static void pascalTriangle(int n) {
        int n2 = n;
        for (int i = 0; i < n; i++) {
            for (int space = 8 * (n2 - 1); space >= 0; space--) {
                System.out.printf(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.printf("%14.0f", combine(i, j));
                System.out.printf("  ");
            }
            System.out.println();
            n2--;
        }
    }

    public static void main(String[] args)
            throws IOException, InterruptedException {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Number of Lines(n): ");
        int n = sc.nextInt();
        pascalTriangle(n);
        System.out.println("Press any key to exit! ");
        sc.nextByte();
    }
}

【讨论】:

    【解决方案2】:

    试试这个...

    结果:

    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  7  21  35  35  21  7  1 
    
    import java.util.*;
    public class HelloWorld {
        static int binCoeff(int n, int k) {
            int res = 1;
            if (k > n - k)
                k = n - k;
            for (int i = 0; i < k; ++i) {
                res *= (n - i);
                res /= (i + 1);
            }
            return res;
        }
    
        static void pascalTriangle(int lines) {
            for (int i = 0; i < lines; i++) {
                for (int j = 0; j <= i; j++)
                    System.out.print(HelloWorld.binCoeff(i, j) + "  ");
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            System.out.println("Results: ");
            HelloWorld.pascalTriangle(8);
        }
    }
    

    【讨论】:

      【解决方案3】:
      /**
       * @author Ranjith
       */
      public class JavaApplication2 {
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              int i;
              int x;
              int n = 15; //number of rows
              String newLine = System.getProperty("line.separator");
              for (i = 0; i < n; i++) { //loop to adjust spacing
                  x = i;
                  while (x < n - 1) {
                      System.out.print(" ");
                      x++;
                  }
                  fib(i); //fibonacci function is called
                  System.out.print(newLine);
              }
          }
          public static void fib(int num) { //fibonacci function
              int[] febo = new int[100];
              febo[0] = 0;
              febo[1] = 1;
              for (int i = 2; i < num; i++) {
                  febo[i] = febo[i - 1] + febo[i - 2];
              }
              for (int i = 0; i < num; i++) {
                  System.out.print(febo[i] + "  ");
              }
          }
      }
      

      输出:

                   0  
                  0  1  
                 0  1  1  
                0  1  1  2  
               0  1  1  2  3  
              0  1  1  2  3  5  
             0  1  1  2  3  5  8  
            0  1  1  2  3  5  8  13  
           0  1  1  2  3  5  8  13  21  
          0  1  1  2  3  5  8  13  21  34  
         0  1  1  2  3  5  8  13  21  34  55  
        0  1  1  2  3  5  8  13  21  34  55  89  
       0  1  1  2  3  5  8  13  21  34  55  89  144  
      0  1  1  2  3  5  8  13  21  34  55  89  144  233  
      

      【讨论】:

        【解决方案4】:

        您可以将这样的三角形表示为二维数组,其中第一行和第一列的元素都等于一个,所有其他元素都是行和列中前一个元素的总和。

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

        然后你可以将它定位在左上角如下:

         1  1  1  1  1  1  1  1  1 
         1  2  3  4  5  6  7  8 
         1  3  6 10 15 21 28 
         1  4 10 20 35 56 
         1  5 15 35 70 
         1  6 21 56 
         1  7 28 
         1  8 
         1 
        

        Try it online!

        public static void main(String[] args) {
            int n = 9;
            // 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];
                    }
                }
            }
        
            // formatted output
            for (int[] row : arr) {
                for (int el : row) {
                    // formatting as a number with a trailing space
                    System.out.printf("%2d ", el); // two-digit number
                    // System.out.printf("%3d ", el); // three-digit number
                    // System.out.printf("%4d ", el); // four-digit number
                }
                System.out.println();
            }
        }
        

        另见:
        Pascal's triangle 2d array - formatting printed output
        Print Pascal's Triangle

        【讨论】:

          【解决方案5】:
          class pascal {
              static void main(int n) {
                  int a[][] = new int[n][n + 1];
                  for (int i = 0; i < n; i++) {
                      for (int j = 0; j < n; j++) {
                          a[i][j] = 0;
                      }
                  }
                  a[0][1] = 1;
                  int k = 5;
                  int p = 0;
                  for (int i = 1; i < n; i++) {
                      for (int j = 1; j < n + 1; j++) {
                          a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
                      }
                  }
                  for (int i = 0; i < a.length; i++) {
                      for (p = n + -i; p > 0; p--) {
                          System.out.print(" ");
                      }
                      for (int j = 0; j < a[i].length; j++) {
                          if (a[i][j] != 0) {
                              System.out.print(a[i][j] + " ");
                          } else {
                              System.out.print(" ");
                          }
                      }
                      System.out.println();
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-12-28
            • 2012-07-13
            • 1970-01-01
            • 2012-10-24
            • 1970-01-01
            • 1970-01-01
            • 2012-09-06
            相关资源
            最近更新 更多