用 python 小程序 来 模拟 高考 概率题 (甲卷选择题
高考已经结束了,同学们面临专业选择的问题,可能一部分同学要选择计算机相关专业。
高考因为时间限制问题,概率题在合理时间内,都可以计算出来,现实的概率问题,有可能比较复杂,需要用计算机模拟,才能计算出来,下边 就发一段 python 程序,来模拟 高考的 概率选择题,看看结果。 先看问题:
再贴python程序截图,直接贴文本程序,把python缩进搞没了,所以贴截图
以下是程序文本呀,缩进没了
import random
numlist=[1,1,1,1,0,0] #将4个1,2个0, 放入 list 中
totalcount=0
continuecount=0
while(1):
random.shuffle(numlist) #生成一个随机排列
totalcount=totalcount+1
for j in range(0,5): #测试两个0是否相邻
if(numlist[j]==0) and (numlist[j+1]==0):
continuecount+=1
break
discontcount=totalcount-continuecount
if(totalcount==100000):
print ("\n 当前一共进行了 %d 次试验" %(totalcount))
print ("\n 当前两个0不相邻的次数为 %d" % (discontcount))
print ("\n 当前不相邻的概率为 %f" %(discontcount/totalcount))
break
再贴运行结果:
这个小程序,能不能提高大家对计算机编程的兴趣呢?