Python程序使用numpy打印n * n的检查板图案
给定n的值,我们的任务是显示anxn矩阵的检查板图案。
numpy提供了用于创建具有初始值的数组的不同类型的函数。NumPy是Python中科学计算的基本软件包。
算法
Step 1: input order of the matrix. Step 2: create n*n matrix using zeros((n, n), dtype=int). Step 3: fill with 1 the alternate rows and columns using the slicing technique. Step 4: print the matrix.
范例程式码
import numpy as np
def checkboardpattern(n):
print("Checkerboard pattern:")
x = np.zeros((n, n), dtype = int)
x[1::2, ::2] = 1
x[::2, 1::2] = 1
# print the pattern
for i in range(n):
for j in range(n):
print(x[i][j], end =" ") print()# Driver code
n = int(input("Enter value of n ::>"))
checkboardpattern(n)输出结果
Enter value of n ::>4 Checkerboard pattern: 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西