Kim tự tháp
Đưa ra một số nguyên
Dòng đầu vào đầu tiên là một số nguyên
Trong ví dụ, số lượng hàng trong kim tự tháp là
5. Vì vậy, đầu ra phải
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
Tìm hiểu thêm về sự giúp đỡ của chúng tôi với các bài tập: Python
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
N = int[input[]]
for i in range[N]:
print['. ' * [N - i - 1], end='']
print['0 ' * [2 * i + 1], end='']
print['. ' * [N - i - 1], end='']
print[]
Trong vòng lặp đầu tiên, chúng tôi lặp lại từ i = 0 đến i = hàng ..
Trong vòng lặp đầu tiên, chúng tôi lặp lại từ i = 0 đến i = hàng ..
Trong vòng lặp thứ hai, chúng tôi in các số bắt đầu từ 1 đến J, trong đó J dao động từ 0 đến i ..
* * * * * * * * * * * * * * *
Mã nguồn
rows = int[input["Enter number of rows: "]]
for i in range[rows]:
for j in range[i+1]:
print["* ", end=""]
print["\n"]
Sau mỗi lần lặp của vòng lặp đầu tiên, chúng tôi in một dòng mới ..
- Làm cách nào để in một chương trình mẫu Python?
- Các mẫu có thể được in bằng Python bằng cách sử dụng đơn giản cho các vòng lặp. Vòng lặp bên ngoài đầu tiên được sử dụng để xử lý số lượng hàng và vòng lồng bên trong được sử dụng để xử lý số lượng cột. Thao tác các câu lệnh in, các mẫu số khác nhau, mẫu bảng chữ cái hoặc mẫu sao có thể được in.
- Các chương trình in hình tam giác bằng cách sử dụng *, số và ký tự
- Ví dụ 1: Chương trình in một nửa kim tự tháp bằng cách sử dụng *
Trong chương trình trên, hãy xem mẫu được in như thế nào.
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Mã nguồn
rows = int[input["Enter number of rows: "]]
for i in range[rows]:
for j in range[i+1]:
print[j+1, end=" "]
print["\n"]
Sau mỗi lần lặp của vòng lặp đầu tiên, chúng tôi in một dòng mới ..
- Làm cách nào để in một chương trình mẫu Python?
- Các mẫu có thể được in bằng Python bằng cách sử dụng đơn giản cho các vòng lặp. Vòng lặp bên ngoài đầu tiên được sử dụng để xử lý số lượng hàng và vòng lồng bên trong được sử dụng để xử lý số lượng cột. Thao tác các câu lệnh in, các mẫu số khác nhau, mẫu bảng chữ cái hoặc mẫu sao có thể được in.
- Các chương trình in hình tam giác bằng cách sử dụng *, số và ký tự
- Ví dụ 1: Chương trình in một nửa kim tự tháp bằng cách sử dụng *
Trong chương trình trên, hãy xem mẫu được in như thế nào.
A B B C C C D D D D E E E E E
Mã nguồn
rows = int[input["Enter number of rows: "]]
ascii_value = 65
for i in range[rows]:
for j in range[i+1]:
alphabet = chr[ascii_value]
print[alphabet, end=" "]
ascii_value += 1
print["\n"]
Đầu tiên, chúng tôi nhận được chiều cao của kim tự tháp
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
4 từ người dùng.Trong vòng đầu tiên, chúng tôi lặp lại từ Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
5 đến Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
6.
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
Vòng lặp thứ hai chạy từ J = 0 đến I + 1. Trong mỗi lần lặp của vòng lặp này, chúng tôi in Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
7 số Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
8 mà không có dòng mới. Ở đây, số hàng cung cấp số Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
8 yêu cầu phải được in trên hàng đó. Ví dụ: ở hàng thứ 2, chúng tôi in hai Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
8. Tương tự, trong hàng thứ 3, chúng tôi in ba Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
8.
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
0Mã nguồn
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
1Khi vòng lặp bên trong kết thúc, chúng tôi in dòng mới và bắt đầu in * trong một dòng mới.
Ví dụ 2: Chương trình in một nửa kim tự tháp A bằng cách sử dụng các số
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
2Mã nguồn
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
3Trong vòng lặp thứ hai, chúng tôi in các số bắt đầu từ
N = int[input[]]
for i in range[N]:
print['. ' * [N - i - 1], end='']
print['0 ' * [2 * i + 1], end='']
print['. ' * [N - i - 1], end='']
print[]
5 đến N = int[input[]]
for i in range[N]:
print['. ' * [N - i - 1], end='']
print['0 ' * [2 * i + 1], end='']
print['. ' * [N - i - 1], end='']
print[]
6, trong đó N = int[input[]]
for i in range[N]:
print['. ' * [N - i - 1], end='']
print['0 ' * [2 * i + 1], end='']
print['. ' * [N - i - 1], end='']
print[]
6 dao động từ N = int[input[]]
for i in range[N]:
print['. ' * [N - i - 1], end='']
print['0 ' * [2 * i + 1], end='']
print['. ' * [N - i - 1], end='']
print[]
8 đến N = int[input[]]
for i in range[N]:
print['. ' * [N - i - 1], end='']
print['0 ' * [2 * i + 1], end='']
print['. ' * [N - i - 1], end='']
print[]
9.Sau mỗi lần lặp của vòng lặp đầu tiên, chúng tôi in một dòng mới.
Ví dụ 3: Chương trình in một nửa kim tự tháp bằng cách sử dụng bảng chữ cái
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
4Mã nguồn
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
5Hoạt động của ví dụ trên cũng tương tự như các ví dụ khác được thảo luận ở trên ngoại trừ các giá trị ASCII được in ở đây. Giá trị ASCII cho bảng chữ cái bắt đầu từ 65 [tức là A]. Do đó, trong mỗi lần lặp, chúng tôi tăng giá trị của
* * * * * * * * * * * * * * *0 và in bảng chữ cái tương ứng của nó.
- Các chương trình để in nửa kim tự tháp đảo ngược bằng cách sử dụng * và số
- Ví dụ 4: Một nửa kim tự tháp đảo ngược bằng cách sử dụng *
- Vòng lặp trong khi in các ngôi sao số yêu cầu sử dụng công thức
* * * * * * * * * * * * * * *
8. Công thức này cung cấp số lượng sao cho mỗi hàng, trong đó hàng là
9.N = int[input[]] for i in range[N]: print['. ' * [N - i - 1], end=''] print['0 ' * [2 * i + 1], end=''] print['. ' * [N - i - 1], end=''] print[]
Ví dụ 7: Kim tự tháp đầy đủ của các số
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
6Mã nguồn
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
7Giống như ví dụ 6, ví dụ này cũng sử dụng hai vòng bên trong một vòng lặp.
- Bên ngoài cho vòng lặp lặp qua mỗi hàng.
- Ở đây chúng tôi sử dụng hai bộ đếm
0 vàrows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
1 để in các không gian và số tương ứng.rows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
- Bên trong cho vòng lặp in các không gian cần thiết bằng công thức
* * * * * * * * * * * * * * *
6, trong đó các hàng là tổng số hàng và
9 là số hàng hiện tại.N = int[input[]] for i in range[N]: print['. ' * [N - i - 1], end=''] print['0 ' * [2 * i + 1], end=''] print['. ' * [N - i - 1], end=''] print[]
- Vòng lặp trong khi in các số trong đó
* * * * * * * * * * * * * * *
8 cung cấp số lượng mục trong mỗi hàng.
Ví dụ 8: Kim tự tháp đầy đủ đảo ngược của *
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
8Mã nguồn
rows = int[input['Enter number of rows here: ']]
for i in range[rows]:
j = i + 1
m = 1 + [2*i]
print['. '*[rows-j]+ '0 ' * m+ '. '*[rows-j]]
Enter number of rows here: 5
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Enter number of rows here: 6
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
9Giống như ví dụ 6, ví dụ này cũng sử dụng hai vòng bên trong một vòng lặp.
- Bên ngoài cho vòng lặp lặp qua mỗi hàng.
- Ở đây chúng tôi sử dụng hai bộ đếm
0 vàrows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
1 để in các không gian và số tương ứng.rows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
- Bên trong cho vòng lặp in các không gian cần thiết bằng công thức
* * * * * * * * * * * * * * *
6, trong đó các hàng là tổng số hàng và
9 là số hàng hiện tại.N = int[input[]] for i in range[N]: print['. ' * [N - i - 1], end=''] print['0 ' * [2 * i + 1], end=''] print['. ' * [N - i - 1], end=''] print[]
Vòng lặp trong khi in các số trong đó *
* *
* * *
* * * *
* * * * *
8 cung cấp số lượng mục trong mỗi hàng.
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
0Mã nguồn
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
1Giống như ví dụ 6, ví dụ này cũng sử dụng hai vòng bên trong một vòng lặp.
- Bên ngoài cho vòng lặp lặp qua mỗi hàng.
- Ở đây chúng tôi sử dụng hai bộ đếm
0 vàrows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
1 để in các không gian và số tương ứng.rows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
- Bên trong cho vòng lặp in các không gian cần thiết bằng công thức
* * * * * * * * * * * * * * *
6, trong đó các hàng là tổng số hàng và
9 là số hàng hiện tại.N = int[input[]] for i in range[N]: print['. ' * [N - i - 1], end=''] print['0 ' * [2 * i + 1], end=''] print['. ' * [N - i - 1], end=''] print[]
Vòng lặp trong khi in các số trong đó *
* *
* * *
* * * *
* * * * *
8 cung cấp số lượng mục trong mỗi hàng.
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
2Mã nguồn
Pyramid
Given an integer
N, write a program to print a pyramid of N rows as shown below.
Input
The first line of input is an integer
N.
Explanation
In the example, the number of rows in the pyramid is
5.
So, the output should be
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 1
5
Sample Output 1
. . . . 0 . . . .
. . . 0 0 0 . . .
. . 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0
Sample Input 2
6
Sample Output 2
. . . . . 0 . . . . .
. . . . 0 0 0 . . . .
. . . 0 0 0 0 0 . . .
. . 0 0 0 0 0 0 0 . .
. 0 0 0 0 0 0 0 0 0 .
0 0 0 0 0 0 0 0 0 0 0
3Giống như ví dụ 6, ví dụ này cũng sử dụng hai vòng bên trong một vòng lặp.
- Bên ngoài cho vòng lặp lặp qua mỗi hàng.
- Ở đây chúng tôi sử dụng hai bộ đếm
0 vàrows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
1 để in các không gian và số tương ứng.rows = int[input["Enter number of rows: "]] for i in range[rows]: for j in range[i+1]: print["* ", end=""] print["\n"]
- Bên trong cho vòng lặp in các không gian cần thiết bằng công thức
* * * * * * * * * * * * * * *
6, trong đó các hàng là tổng số hàng và
9 là số hàng hiện tại.N = int[input[]] for i in range[N]: print['. ' * [N - i - 1], end=''] print['0 ' * [2 * i + 1], end=''] print['. ' * [N - i - 1], end=''] print[]