1. 程式人生 > >課堂作業之楊輝三角形

課堂作業之楊輝三角形

.cn asc != while 設置 height java import 遍歷數組

楊輝三角形數學模型

    1
   11
  121
 1331

通過java實現打印楊輝三角形

用循環隊列來實現楊輝三角形的打印

package PascalTriangle;

import javafoundations.CircularArrayQueue;

import java.util.Scanner;

public class YHTriangleQueue {
    public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    System.out.print("請輸入楊輝三角的行數:");
    int n = scan.nextInt();
    int i = 0;

    CircularArrayQueue<Integer> queue = new CircularArrayQueue<>();
    //初始值設置為 0,1
    queue.enqueue(0);
    queue.enqueue(1);

    while (i <= n) {
        int x = queue.dequeue();
        int y = queue.first();
        if (x == 0) {
            i++;  //每次 x = 0 時行數加一
            queue.enqueue(0);
        }
        queue.enqueue(x + y);  //上一輪輸出值與檢測值之和,相當於前一行相鄰的兩元素之和
        if (x == 0) {
            System.out.println();  //刪除 0 相當於換行
            for (int j = 0; j < 2 * (n - i); j++) {  //根據具體變化動態輸出空字符串
                System.out.print(" ");
            }
        } else
            System.out.print(x + "   ");  //根據具體變化動態輸出空字符串
        }
    }
}

技術分享圖片

  • 單步跟蹤
    技術分享圖片

二維數組打印楊輝三角

public class Yanghui1 {
public static void main(String[] args)
{
   int num = 10;             //設置楊輝三角的行數  
   int[][] yangHui = new int [num][];          //申請二維數組存放楊輝三角數值
   for(int i = 0; i < yangHui.length; i ++)
   yangHui[i] = new int[i + 1];

for(int i = 0; i < yangHui.length; i ++)       //利用楊輝三角的計算公式,初始化數組
  for(int j = 0; j < yangHui[i].length; j ++)
  {
    yangHui[i][0] = yangHui[i][i] = 1;        //每一行第一個數和最後一個數都為1
    if(i > 1 && j > 0 && j < i)
      yangHui[i][j] = yangHui[i - 1][j] + yangHui[i -1][j -1];  //楊輝三角的值等於其上一層兩個值之和
  }

for(int i = 0; i < yangHui.length; i ++)    //遍歷數組,以輸出
    {
  for(int j = 0; j < (num - i - 1) / 2; j ++)
    System.out.print("\t");
  for(int j = 0; j < yangHui[i].length; j ++)
    System.out.print(yangHui[i][j] + "\t");

  System.out.println();
    }
  }
}

兩個一維數組打印楊輝三角

public class YangHui2    {
  public static void main(String[] args)  
   {
    int num = 20;
    int[] up = new int[num];    //申請UP數組用以存儲上一層的數據
    for(int i = 0; i <= num; i ++)
     {      
      int[] a = new int[i];     //申請a數組用以存放本層的運算結果
      for(int j = 0; j < i; j ++)
      {
        if(j == 0 || j == i)           //將本層第一個和最後一個數值賦值為1
          a[j] = up[j] = 1;

        if(i > 2 && j !=0 && j != i )   //當從第三行起,非首尾數字的值等於上方兩數只和
          a[j] = up[j - 1] + up[j];
      }

      for(int k = 0; k < i; k ++)      //將本層的運算結果存到up數組中,以供一下次運算使用
        up[k] = a[k];

      for(int k = 0; k < (num - i - 1) / 2; k ++)      //打印本層數字
        System.out.print("\t");
      for(int k = 0; k < i; k ++)
        System.out.print(a[k] + "\t");

      System.out.println();
        }
      }
    }

一個一維數組打印楊輝三角

public class YangHui3{
public static void main(String[] args)
  {
    int num = 10;

int yangHui[] = new int[(1 + num) * num / 2];    //申請數組存放楊輝三角的數值
for(int i = 0; i < num; i ++)
{
  for(int j = 0; j < (num - i - 1) / 2; j ++)
    System.out.print("\t");

  int temp = (1 + i) * i / 2;     //temp表示已經存儲的數字個數
  for(int j = 0; j <= i; j ++)
  {
    if(j == 0 || j == i)          //每一層的首尾數值為1
      yangHui[temp + j] = 1;
      
    else        
      yangHui[temp + j] = yangHui[temp + j - i] + yangHui[temp + j - i - 1];//非首尾數值為上面兩數之和
      System.out.print(yangHui[temp + j] + "\t");
  }
}
System.out.println();
  }
}

課堂作業之楊輝三角形