-->
当前位置:首页 > 题库 > 正文内容

函数题:列表推导生成随机数矩阵(高教社,《Python编程基础及应用》习题4-10)

Luz4年前 (2021-11-05)题库976
编写一个Python函数,生成一个m行n列的元素值不大于20的随机数矩阵(二维列表)。
提示:使用random.randint()函数来生成随机数。

### 函数接口定义:
python
def generateMatrix(m,n)


m: 行数,int, 1 < m < 100; n: 列数, int, 1 < n < 100.

### 裁判测试程序样例:
python
import random

m = int(input())
n = int(input())
r = generateMatrix(m,n)
p = True
for i in range(m):
for j in range(n):
if r[i][j] > 20:
p = False
break
if m > 3 and n > 3:
if r[0][0] == r[2][2] and r[1][1] == r[1][2] and r[0][2] == r[3][0]:
p = False

print(p)
#测试程序的输出:
True


### 测试输入样例:

in
5
3


### 测试程序输出:

out
True



<br>**拼尽全力还是不会?参考B站习题讲解**<br>

哔哩哔哩up主:[海洋饼干叔叔](https://space.bilibili.com/384177380) [Python课程](https://www.bilibili.com/video/BV1kt411R7uW/)
[Python习题](https://www.bilibili.com/video/BV1iL411t7UZ/)
[简洁的C和C++](https://www.bilibili.com/video/BV1it411d7zx/)







答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。