测验5: 函数和代码复用 (第5周)
单选题
第三题补充说明,∗∗∗∗**∗∗ 在python中是表示乘方的意思,例如 2∗∗5=322∗∗5=322**5=322∗∗5=32
程序题
题目的意思很简单,就是调用 randomrandomrandomrandom 库中的 randintrandintrandintrandint 函数产生随机数即可
import random
def genpwd(length):
# x,y是取值范围
x=pow(10,length-1)
y=pow(10,length)-1
return random.randint(x,y)
length = eval(input())
random.seed(17)
for i in range(3):
print(genpwd(length))1
2
3
4
5
6
7
8
9
10
11
12
这个题主要是考察判断素数和控制格式输出,判断素数我们直接使用在测验4中已经写过的函数即可,最后一个数后面不要逗号,需要特判一下
import math
def prime(m):
temp=int(math.sqrt(m))
for i in range(2,temp+1):
if(m%i==0):
return False
return True
n = eval(input())
t=int(n)# int()函数是向下取整
if(n!=t):# 如果是10.5这种浮点数,需要向上取整
t+=1
times=5
while(times>0):
if(prime(t)):
times-=1
if(times>=1):# 前四个数有逗号
print(t,end=',')
else:# 最后一个数没有逗号
print(t)
t+=11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22