修改下列程序实现功能: 例2:Sn = 1! + 2! + 3! + 4! + 5! 不改变程序结构 不添加删除语句 只修改语句 ''' i , arr , s = 1 , 1 , 0 while arr < 100: i, arr , s = i + 1 , arr * (i+1) , s + arr print('s =',s) '''E0 分别使用程序语句和列表推导编程实现如下要求: (两者都用到才能得满分,只做1个只能得一半分) 输出如下所示 * 号矩阵 学号为奇数的是做如下所示 * * * * * * * * * 学号为偶数的是做如下所示 * * * * * * * * *
时间: 2024-02-14 13:09:20 浏览: 65
程序语句实现:
```
i, arr, s = 1, 1, 0
while i <= 5:
j, res = 1, 1
while j <= i:
res *= j
j += 1
s += res
i += 1
print("s =", s)
# 输出奇数学号的矩阵
if int(input("请输入你的学号:")) % 2 == 1:
for i in range(5):
for j in range(5):
if i == j or i + j == 4:
print("* ", end="")
else:
print(" ", end="")
print()
# 输出偶数学号的矩阵
else:
for i in range(5):
for j in range(5):
if j == 0 or j == 4 or i == j + 1 or i == j - 1:
print("* ", end="")
else:
print(" ", end="")
print()
```
列表推导实现:
```
s = sum([math.factorial(i) for i in range(1, 6)])
print("s =", s)
# 输出奇数学号的矩阵
if int(input("请输入你的学号:")) % 2 == 1:
[[print("* ", end="") if i == j or i + j == 4 else print(" ", end="") for j in range(5)] and print() for i in range(5)]
# 输出偶数学号的矩阵
else:
[[print("* ", end="") if j == 0 or j == 4 or i == j + 1 or i == j - 1 else print(" ", end="") for j in range(5)] and print() for i in range(5)]
```
阅读全文