Tam giác Pascal là một mảng tam giác của các hệ số nhị thức. Được đặt tên theo nhà toán học Blaise Pascal.
Trong tam giác Pascal, ở hàng đầu tiên có một số 1 duy nhất. Mỗi số của mỗi hàng tiếp theo được xây dựng bằng cách thêm số ở trên và bên trái với số ở trên và sang bên phải Nghĩa là tổng hai số đối diện bên Trên nó
VD tam giác Pascal kích cỡ 7 :
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
Bài này yêu cầu chúng ta in ra một tam giác Pascal kích cỡ n. Chúng ta có thể dùng mảng 2 chiều để lưu các số ở từng hàng, từng cột
Gọi mảng 2 chiều a là mảng lưu các giá trị trong tam giác
Ban đầu gán a[0][0]=1;
Khi đó cho chạy một vòng for từ 1 --> n-1 :
Gán mọi phần tử a[i][0] = 1
chạy thêm một vòng for nữa từ 1 --> n-1 : gán a[i][j] = a[i-1][j] + a[i-1][j-1];
In ra mảng
Code C++:
```
#include <bits/stdc++.h>
const int N=60;
using namespace std;
int n;
long long a[N+5][N+5];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
a[0][0]=1;
for(int i=1;i<n;++i){
a[i][0]=1;
for(int j=1;j<n;++j){
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(a[i][j]!=0)cout<<a[i][j]<<' ';
}cout<<'\n';
}
}
```
### **Pascal's Triangle**
#### **What is Pascal’s Triangle?**
Pascal’s Triangle is a special triangular number pattern where:
- The first row consists of a single **1**.
- Each number in the subsequent rows is the **sum of the two numbers directly above it**.
- The first and last number of each row are always **1**.
#### **Example for n = 5:**
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
---
## **Hints for Solving the Problem**
### **Hint 1: Use a 2D Array**
Since Pascal’s Triangle is naturally structured in rows and columns, a **2D array** can be used to store the values efficiently.
Let’s define an **n × n** matrix `a[n][n]`, initialized with `0`, where:
- Each row `i` contains the values of the **ith row** of Pascal’s Triangle.
### **Hint 2: Consider Small Cases (`n=2,3,4`)**
Observing small cases helps in identifying patterns:
- **For `n = 2`**
```
1
1 1
```
- **For `n = 3`**
```
1
1 1
1 2 1
```
- **For `n = 4`**
```
1
1 1
1 2 1
1 3 3 1
```
From these examples, we observe:
- The first and last elements of each row are always **1**.
- Each middle element is **the sum of the two numbers directly above it**.
---
## **Solution**
We define a **2D array** `a[n][n]` where `a[i][j]` represents the value at the `j`th position in the `i`th row of Pascal’s Triangle. The first column and the diagonal elements are always **1**, i.e., `a[i][0] = a[i][i] = 1` for all `0 ≤ i < n`. For all other elements, each value is obtained as the sum of the two numbers directly above it: `a[i][j] = a[i-1][j-1] + a[i-1][j]` for `1 ≤ j < i`. After constructing the triangle, we print the values in a triangular format.
---
## **Implementation**
```cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<vector<ll>> a(n,vector<ll>(n,0));
a[0][0]=1;
for(int i = 1; i < n; i++)
{
a[i][0]=1;
for(int j = 1; j < n; j++)
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(a[i][j] !=0)
{
cout << a[i][j] << " ";
}
}
cout << "\n";
}
return 0;
}
```