📜  打印星帕斯卡三角形的Java程序

📅  最后修改于: 2022-05-13 01:54:38.807000             🧑  作者: Mango

打印星帕斯卡三角形的Java程序

帕斯卡三角形是二项式系数的三角形阵列。编写一个函数,将整数值 n 作为输入并打印帕斯卡三角形的前 n 行。以下是帕斯卡三角形的前6行。在本文中,我们将研究打印帕斯卡三角形的过程。帕斯卡三角形是基于 nCr 的三角形图案。下面是帕斯卡三角形的图形表示。

插图:

Input : N = 5
Output:
          1
        1   1
      1   2   1
    1   3   3   1
  1   4   6   4   1
1   5   10   10   5   1

方法:

有两种方法可以解决相同的问题。让我们检查一下。



  1. 使用 nCr 公式
  2. 使用二项式系数

方法 1:使用 nCr 公式

实现:按照以下算法使用 nCr 公式打印帕斯卡三角形

  • 令 n 为要打印的行数
  • 使用从 0 到 k 次的外部迭代 a 来打印行
  • 对 b 进行从 0 到 (K – 1) 的内部迭代。
  • 然后将空格打印为“”。
  • 关闭内部“b”循环。
  • 对 b 从 '0' 到 'a' 进行内部迭代。
  • 'a' 和 'b' 的输出 nCr。
  • 关闭内循环。
  • 打印每个内迭代之后字符(\ n)的。

例子

Java
// Java Program to Print Pascal's Triangle
 
// Importing input output classes
import java.io.*;
 
// Main Class
public class GFG {
 
    // Method 1
    // To find factorial of a number
    public int factorial(int a)
    {
        // Edge case
        // Factorial of 0 is unity
        if (a == 0)
 
            // Hence return 1
            return 1;
 
        // else recursively call the function over the
        // number whose facoial is to be computed
        return a * factorial(a - 1);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declare and initialize number whose
        // factorial is to be computed
        int k = 4;
 
        int a, b;
 
        // Creating an object of GFG class
        // in the main() method
        GFG g = new GFG();
 
        // iterating using nested for loop to traverse over
        // matrix
 
        // Outer for loop
        for (a = 0; a <= k; a++) {
 
            // Inner loop 1
            for (b = 0; b <= k - a; b++) {
 
                // Print white space for left spacing
                System.out.print(" ");
            }
 
            // Inner loop 2
            for (b = 0; b <= a; b++) {
 
                // nCr formula
                System.out.print(
                    " "
                    + g.factorial(a)
                          / (g.factorial(a - b)
                             * g.factorial(b)));
            }
 
            // By now, we are done with one row so
            // a new line
            System.out.println();
        }
    }
}


Java
// Java Program to Print Pascal's Triangle
 
// Importing input output classes
import java.io.*;
 
// Main Class
public class GFG {
 
    // Method 1
    // Pascal function
    public static void printPascal(int k)
    {
        for (int line = 1; line <= k; line++) {
            for (int b = 0; b <= k - line; b++) {
 
                // Print whitespace for left spacing
                System.out.print(" ");
            }
 
            // Variable used to represent C(line, i)
            int C = 1;
 
            for (int a = 1; a <= line; a++) {
 
                // The first value in a line is always 1
                System.out.print(C + " ");
               
                C = C * (line - a) / a;
            }
 
            // By now, we are done with one row so
            // a new line is required
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declare and initialize variable number
        // uoto which Pascal's triangle is  required on
        // console
        int n = 6;
 
        // Calling the Pascal function(Method 1)
        printPascal(n);
    }
}


输出
1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1

方法 2:使用二项式系数

行号中的第 'A' 个条目是二项式系数C(line, a)并且所有行都以值 1 开始。想法是使用 C(line, a-1) 计算 C(line, a)。

C(line, i) = C(line, i-1) * (line - i + 1) / i

执行:

例子

Java

// Java Program to Print Pascal's Triangle
 
// Importing input output classes
import java.io.*;
 
// Main Class
public class GFG {
 
    // Method 1
    // Pascal function
    public static void printPascal(int k)
    {
        for (int line = 1; line <= k; line++) {
            for (int b = 0; b <= k - line; b++) {
 
                // Print whitespace for left spacing
                System.out.print(" ");
            }
 
            // Variable used to represent C(line, i)
            int C = 1;
 
            for (int a = 1; a <= line; a++) {
 
                // The first value in a line is always 1
                System.out.print(C + " ");
               
                C = C * (line - a) / a;
            }
 
            // By now, we are done with one row so
            // a new line is required
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declare and initialize variable number
        // uoto which Pascal's triangle is  required on
        // console
        int n = 6;
 
        // Calling the Pascal function(Method 1)
        printPascal(n);
    }
}
输出
1 
     1 1 
    1 2 1 
   1 3 3 1 
  1 4 6 4 1 
 1 5 10 10 5 1