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

javaobject oriented programmingprogramming更新于 2024/4/24 4:28:17

在本文中,我们将了解如何打印星形帕斯卡三角形。帕斯卡三角形由多个 for 循环和打印语句组成。三角形外的所有值都被视为零 (0)。第一行是 0 1 0,而只有 1 在帕斯卡三角形中占据一个空间,0 是不可见的。第二行是通过添加 (0+1) 和 (1+0) 获得的。输出夹在两个零之间。这个过程持续到达到所需的级别。

下面是相同的演示 −

输入

假设我们的输入是 −

输入帕斯卡三角形的行数:8

输出

所需的输出将是 −

假设我们的输入是 −

输入帕斯卡三角形的行数:8
帕斯卡三角形:
        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
1 8 28 56 70 56 28 8 1

算法

步骤 1 - 开始
步骤 2 - 声明三个整数值,即 i、j 和 my_input
步骤 3 - 从用户那里读取所需值/定义值
步骤 4 - 定义一个函数"factorial()"计算两个数字的阶乘,并使用函数"Combination()"计算两个数字的组合
步骤 5 - 我们迭代两个嵌套的"for"循环以获取字符之间的空格。
步骤 6 - 迭代最内层循环后,我们迭代另一个"for"循环。这将有助于获得"i"和"j"的组合值。
步骤 7 - 现在,打印一个换行符以获取后续行中"i"和"j"的组合值的具体数量。
步骤 8 - 显示结果
步骤 9 - 停止

示例 1

import java.util.Scanner;
public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 5;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number of rows in Pascal's Triangle : ");
      my_input = my_scanner.nextInt();
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
        for(j = 0; j <= i; j++){
           System.out.print(" "+combination(i, j));
        }
        System.out.println();
     }
   }
}

输出

Required packages have been imported
A reader object has been defined
Enter the number of rows in Pascal's Triangle : 8
The Pascal's Triangle :
         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
 1 8 28 56 70 56 28 8 1

示例 2

这里,整数已经被预先定义,并且它的值被访问并显示在控制台上。

public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 8;
      System.out.println("The number of rows in Pascal's Triangle is defined as " +my_input);
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
         for(j = 0; j <= i; j++){
            System.out.print(" "+combination(i, j));
         }
         System.out.println();
      }
   }
}

输出

The number of rows in Pascal's Triangle is defined as 8
The Pascal's Triangle :
        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
1 8 28 56 70 56 28 8 1

相关文章


有用资源