Giới thiệu
Trong bài viết này, chúng ta sẽ tìm hiểu cách in tam giác Pascal sao (star Pascal's triangle). Tam giác Pascal được tạo bằng nhiều vòng lặp for lồng nhau và lệnh in. Tất cả giá trị ngoài tam giác coi là 0. Hàng đầu là 0 1 0 (chỉ 1 hiển thị). Hàng thứ hai là (0+1) và (1+0). Kết quả được kẹp giữa 2 số 0. Quá trình lặp lại cho đến cấp độ yêu cầu.
Minh họa
Đầu vào:
Enter the number of rows in Pascal's Triangle : 8
Đầu ra:
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
Thuật toán
- Bắt đầu
- Khai báo 3 số nguyên:
i,j,my_input - Đọc giá trị từ người dùng hoặc định nghĩa trước
- Định nghĩa hàm
factorial()tính giai thừa vàCombination()tính tổ hợp - Duyệt 2 vòng lặp
forlồng nhau để tạo khoảng trắng - Sau vòng lặp trong cùng, duyệt thêm vòng
forin giá trị tổ hợp C(i, j) - In dòng mới cho các dòng tiếp theo
- Hiển thị kết quả
- Kết thúc
Ví dụ 1: Nhập từ bàn phím (Scanner)
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();
}
}
}
Đầu ra
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
Ví dụ 2: Giá trị định nghĩa trước
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();
}
}
}
Đầu ra
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