Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java พิมพ์สามเหลี่ยมปาสคาล (Pascal's Triangle)

การพิมพ์สามเหลี่ยมปาสคาล (Pascal's Triangle) ใน Java

สามเหลี่ยมปาสคาลเป็นรูปแบบตัวเลขที่แต่ละตัวเป็นผลรวมของสองตัวเหนือมัน ใช้ Combination formula: C(n, k) = n! / (k! * (n-k)!) หรือคำนวณแบบ Iterative

หลักการคณิตศาสตร์

  • แถวที่ n (0-indexed) มี n+1 ตัวเลข
  • ค่าที่ตำแหน่ง k คือ C(n, k) (Combination)
  • ขอบซ้ายและขวาเป็น 1 เสมอ
  • ค่ากึ่งกลาง = ผลรวมสองค่าด้านบน

ตัวอย่างที่ 1: ใช้ Factorial และ Combination (ต้นฉบับ)

import java.util.Scanner;

public class PascalsTriangle {
    static int factorial(int n) {
        int fact = 1;
        for (; n > 1; n--) {
            fact *= n;
        }
        return fact;
    }
    
    static int combination(int n, int r) {
        return factorial(n) / (factorial(n - r) * factorial(r));
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("ป้อนจำนวนแถว: ");
        int n = scanner.nextInt();
        
        System.out.println("สามเหลี่ยมปาสคาล:");
        
        for (int i = 0; i <= n; i++) {
            // ช่องว่างหน้า
            for (int j = 0; j <= n - i; j++) {
                System.out.print(" ");
            }
            
            // ค่าคอมบิเนชัน
            for (int j = 0; j <= i; j++) {
                System.out.print(" " + combination(i, j));
            }
            System.out.println();
        }
    }
}

ตัวอย่างที่ 2: วิธี Iterative (มีประสิทธิภาพกว่า, ไม่ต้อง Factorial)

public class PascalsTriangleIterative {
    public static void main(String[] args) {
        int n = 8;
        System.out.println("จำนวนแถว: " + n);
        System.out.println("สามเหลี่ยมปาสคาล:");
        
        for (int i = 0; i < n; i++) {
            System.out.print(" ".repeat(n - i - 1));
            
            int val = 1;
            for (int j = 0; j <= i; j++) {
                System.out.print(val + " ");
                // คำนวณค่าถัดไป: C(i, j+1) = C(i, j) * (i-j) / (j+1)
                val = val * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

ตัวอย่างที่ 3: ใช้ List สร้างแถวจากแถวก่อนหน้า (Dynamic Programming)

import java.util.*;

public class PascalsTriangleDP {
    public static void main(String[] args) {
        int n = 8;
        List> triangle = new ArrayList<>();
        
        for (int i = 0; i < n; i++) {
            List row = new ArrayList<>(i + 1);
            row.add(1); // ตัวแรกเสมอเป็น 1
            
            if (i > 0) {
                List prevRow = triangle.get(i - 1);
                for (int j = 1; j < i; j++) {
                    row.add(prevRow.get(j - 1) + prevRow.get(j));
                }
                row.add(1); // ตัวสุดท้ายเสมอเป็น 1
            }
            
            triangle.add(row);
        }
        
        // พิมพ์
        for (int i = 0; i < n; i++) {
            System.out.print(" ".repeat(n - i - 1));
            for (int val : triangle.get(i)) {
                System.out.print(val + " ");
            }
            System.out.println();
        }
    }
}

ผลลัพธ์ (n=6)

สามเหลี่ยมปาสคาล:
     1 
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 
1 5 10 10 5 1

การปรับปรุง: ใช้ StringBuilder (Java 11+)

public class PascalsTriangleModern {
    public static void main(String[] args) {
        int n = 8;
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < n; i++) {
            sb.append(" ".repeat(n - i - 1));
            
            int val = 1;
            for (int j = 0; j <= i; j++) {
                sb.append(val).append(" ");
                val = val * (i - j) / (j + 1);
            }
            sb.append("\n");
        }
        
        System.out.print(sb.toString());
    }
}

การวิเคราะห์ประสิทธิภาพ

วิธี Time Space หมายเหตุ
Factorial + Combination O(n³) O(1) คำนวณ factorial ซ้ำมาก Overflow ง่าย
Iterative Formula O(n²) O(1) เร็วที่สุด ไม่ overflow ง่าย
DP (List of Lists) O(n²) O(n²) เก็บทุกแถว ใช้ได้ถ้าต้องเข้าถึงภายหลัง

คุณสมบัติน่าสนใจ

  • แถวที่ n ผลรวม = 2^n
  • สัมประสิทธิ์ของ (a+b)^n
  • แนวทแยงมุม: 1, จำนวนธรรมชาติ, จำนวนสามเหลี่ยม, จำนวนสี่หน้า...
  • รูปแบบ Sierpinski triangle หากทำสีคู่/คี่