###### YOHOL56

2021-05-09 13:31 阅读 163

# 请求各位看看这个python程序错在了哪里，为什么运行不了？

50

#下列程序注释中所述奇列数列及偶列数列是指实际生活中数学意义上的矩阵列数
import random
m,n=eval(input('请输入行数与偶数列数'))
list1=[]
list2=[]
list3=[]
list4=[]
type1=0
s=int(m*n/2)
l=0
c=total=1
for i in range(1,m*n+1):                               #计算所有方案数的总数（阶乘）
total=total*i
total=total//((2**(m*n//2)))
for h in range(1,total+1):                           #矩阵个数
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for i in range(m):
for j in range(n):
if j%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for j in range(m):
for i in range(n):
if i%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
while type1!=total:
for h in range(1,total-type1+1):                             #矩阵个数
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for i in range(m):
for j in range(n):
if j%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
x=[[0]*n for i in range(m)]                           #定义矩阵
for j in range(m):
for i in range(n):
if i%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
for y in list3:
for i in range(m):                                    #判断相邻数是否相同
for j in range(n):
if i==0 and 1<=j<=n-2:
if x[i][j]==x[i][j-1] or x[i][j]==x[i][j+1] or x[i][j]==x[i+1][j] :
l=l+1
if i==0 and j==0:
if x[i][j]==x[i+1][j]or x[i][j]==x[i][j+1]:
l=l+1
if i==0 and j==n-1:
if x[i][j]==x[i+1][j]or x[i][j]==x[i][j-1]:
l=l+1
if i==m-1 and j==0:
if x[i][j]==x[i-1][j]or x[i][j]==x[i][j+1]:
l=l+1
if i==m-1 and j==n-1:
if x[i][j]==x[i-1][j]or x[i][j]==x[i][j-1]:
l=l+1
if 1<=i<=m-2 and j==0:
if x[i][j]==x[i-1][j]or x[i][j]==x[i+1][j] or x[i][j]==x[i][j+1]:
l=l+1
if i==m-1 and 1<=j<=n-2:
if x[i][j]==x[i][j+1]or x[i][j]==x[i][j-1]or x[i][j]==x[i-1][j]:
l=l+1
if 1<=i<=m-2 and j==n-1:
if x[i][j]==x[i-1][j]or x[i][j]==x[i+1][j]or x[i][j]==x[i][j-1]:
l=l+1
if 1<=i<=m-2 and 1<=j<=n-2:
if x[i][j]==x[i][j+1] or x[i][j]==x[i+1][j] or x[i][j]==x[i][j-1] or x[i][j]==x[i-1][j]:
l=l+1
if l==m*n:
list4.append(x)
for i in range(1,(m*n//2)+1):                               #计算所有方案数的总数（阶乘）
c=c*i
print('恰覆盖方案数为',int(len(list3)/c))
#print(list3)
print(list4)

• 点赞
• 收藏
• 复制链接分享

#### 6条回答默认 最新

• 白驹_过隙 2021-05-09 15:29
``````import random
m=eval(input('请输入行数'))
n=eval(input('请输入偶数列数'))
list1=[]
list2=[]
list3=[]
list4=[]
type1=0
s=int(m*n/2)
l=0
c=total=1
for i in range(1,m*n+1):                               #计算所有方案数的总数（阶乘）
total=total*i
total=total//((2**(m*n//2)))
for h in range(1,total+1):                           #矩阵个数
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for i in range(m):
for j in range(n):
if j%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for j in range(m):
for i in range(n):
if i%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
while type1!=total:
for h in range(1,total-type1+1):                             #矩阵个数
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for i in range(m):
for j in range(n):
if j%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
x=[[0]*n for i in range(m)]                           #定义矩阵
for j in range(m):
for i in range(n):
if i%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
for y in list3:
for i in range(m):                                    #判断相邻数是否相同
for j in range(n):
if i==0 and 1<=j<=n-2:
if x[i][j]==x[i][j-1] or x[i][j]==x[i][j+1] or x[i][j]==x[i+1][j] :
l=l+1
if i==0 and j==0:
if x[i][j]==x[i+1][j]or x[i][j]==x[i][j+1]:
l=l+1
if i==0 and j==n-1:
if x[i][j]==x[i+1][j]or x[i][j]==x[i][j-1]:
l=l+1
if i==m-1 and j==0:
if x[i][j]==x[i-1][j]or x[i][j]==x[i][j+1]:
l=l+1
if i==m-1 and j==n-1:
if x[i][j]==x[i-1][j]or x[i][j]==x[i][j-1]:
l=l+1
if 1<=i<=m-2 and j==0:
if x[i][j]==x[i-1][j]or x[i][j]==x[i+1][j] or x[i][j]==x[i][j+1]:
l=l+1
if i==m-1 and 1<=j<=n-2:
if x[i][j]==x[i][j+1]or x[i][j]==x[i][j-1]or x[i][j]==x[i-1][j]:
l=l+1
if 1<=i<=m-2 and j==n-1:
if x[i][j]==x[i-1][j]or x[i][j]==x[i+1][j]or x[i][j]==x[i][j-1]:
l=l+1
if 1<=i<=m-2 and 1<=j<=n-2:
if x[i][j]==x[i][j+1] or x[i][j]==x[i+1][j] or x[i][j]==x[i][j-1] or x[i][j]==x[i-1][j]:
l=l+1
if l==m*n:
list4.append(x)
for i in range(1,(m*n//2)+1):                               #计算所有方案数的总数（阶乘）
c=c*i
print('恰覆盖方案数为',int(len(list3)/c))
#print(list3)
print(list4)``````
点赞 评论 复制链接分享
• 小小吴"k" 2021-05-09 15:34

eval将字符串str当成有效的表达式来求值并返回计算结果。它要执行的python代码只能是单个运算表达式（不支持任意形式的赋值操作），而不能是复杂的代码逻辑。

eval(expression, globals=None, locals=None)

点赞 评论 复制链接分享
• 冷寒越 2021-05-09 15:34

首先    n和m 需要单独输入，不然只有n转换被转换，m还是str类型

第二   你这个代码m 和 n不等的情况下，中间列表会超范围

点赞 评论 复制链接分享
• 有问必答小助手 2021-05-10 11:05

您好，我是有问必答小助手，您的问题已经有小伙伴解答了，您看下是否解决，可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案，可以点击【采纳】按钮，给回答的小伙伴一些鼓励哦～～

点赞 评论 复制链接分享
• 有问必答小助手 2021-05-11 15:52

非常感谢您使用有问必答服务，为了后续更快速的帮您解决问题，现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中，希望能得到您的支持与协助！

速戳参与调研>>>https://t.csdnimg.cn/Kf0y

点赞 评论 复制链接分享
• 一切因为有你 2021-05-12 16:58
``````import random
m=int(input('请输入行数'))
n=int(input('请输入偶数列数'))
list1=[]
list2=[]
list3=[]
list4=[]
type1=0
s=int(m*n/2)
l=0
c=total=1
for i in range(1,m*n+1):                               #计算所有方案数的总数（阶乘）
total=total*i
total=total//((2**(m*n//2)))
for h in range(1,total+1):                           #矩阵个数
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for i in range(m):
for j in range(n):
if j%2==0:                                     #奇列数列随机从列表1中取值并删除
x[i][j]=random.choice(list1)
list1.remove(x[i][j])
else:
x[i][j]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[i][j])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1
for i in range(1,s+1,1):
list1.append(i)
for i in range(1, s+1 , 1):
list2.append(i)
x=[[0]*n for i in range(m)]                           #定义矩阵
for j in range(m):
for i in range(n):
if i%2==0:                                     #奇列数列随机从列表1中取值并删除
x[j][i]=random.choice(list1)
list1.remove(x[j][i])
else:
x[j][i]= random.choice(list2)          #偶列数列随机从列表2中取值并删除
list2.remove(x[j][i])
list3.append(x)
if list3.count(x)>1:
list3.remove(x)
else:
type1=type1+1``````
点赞 评论 复制链接分享