首页 > 其他分享 >模拟体育竞技

模拟体育竞技

时间:2023-12-28 23:00:14浏览次数:21  
标签:winsB winsA scoreA scoreB probB probA 体育竞技 模拟

# 打印程序介绍信息
def printIntro():
print("学号43")
print("这是乒乓球个人赛模拟程序:")


# 获得程序运行参数
def getInputs():
a = eval(input("请输入队伍A的能力值(0-1): "))
b = eval(input("请输入队伍B的能力值(0-1): "))
n = eval(input("模拟比赛的场次: "))
return a, b, n


# 进行N场比赛
def simNGames(n, probA, probB):
winsA, winsB = 0, 0
for i in range(n):
for j in range(7): # 进行7局4胜的比赛
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB


# 正常比赛结束
def gameOver(a, b):
return a == 11 or b == 11


def gameOver2(a, b): # 进行抢12比赛结束
return a == 12 or b == 12


# 进行一场比赛
def simOneGame(probA, probB):
scoreA, scoreB = 0, 0 # 初始化AB的得分
serving = "A"
while not gameOver(scoreA, scoreB): # 用while循环来执行比赛
if scoreA == 10 and scoreB == 10:
return(simtwoGame2(probA,probB))
if serving == "A":
if random() < probA: # 用随机数生成胜负
scoreA += 1
else:
serving = "B"
else:
if random() < probB:
scoreB += 1
else:
serving="A"
return scoreA, scoreB


def simtwoGame2(probA,probB):
scoreA, scoreB=10, 10
serving = "A" # 假如先让队伍A发球
while not gameOver2(scoreA, scoreB):
if serving == "A":
if random() < probA:
scoreA += 1
else:
serving = "B"
else:
if random() < probB:
scoreB += 1
else:
serving = "A"
return scoreA, scoreB


def printSummary(winsA, winsB):
n = winsA + winsB
print("竞技分析开始,共模拟{}场比赛".format(n//7))
print("A获胜{}场比赛,占比{:0.1%}".format(winsA//7+1, winsA/n))
print("B获胜{}场比赛,占比{:0.1%}".format(winsB//7, winsB/n))


def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)


main()

 

#先到100分获胜
from random import random
def printIntro():
print("by 43")
print("这个程序模拟两个队A和B的篮球比赛")
print("程序运行需要队A和队B的能力值(以0到1之间的小数表示)")
def getInputs():
a = eval(input("请输入队A的能力值(0-1): "))
b = eval(input("请输入队B的能力值(0-1): "))
n = eval(input("模拟比赛的场次: "))
return a, b, n
def simNGames(n, probA, probB):
winsA, winsB = 0, 0
for i in range(n):
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB
def gameOver(a,b):
return a==100 or b==100
def simOneGame(probA, probB):
scoreA, scoreB = 0, 0
serving = "A"
while not gameOver(scoreA, scoreB):
if serving == "A":
if random() < probA:
scoreA += 1
else:
scoreB += 1
else:
if random() < probB:
scoreB += 1
else:
scoreA += 1
return scoreA, scoreB
def printSummary(winsA, winsB):
n = winsA + winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("队A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
print("队B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)
main()

 

用pyinstaller打包程序

 

 

标签:winsB,winsA,scoreA,scoreB,probB,probA,体育竞技,模拟
From: https://www.cnblogs.com/fmhqq/p/17933780.html

相关文章

  • 比赛模拟
    fromrandomimportrandom#打印程序介绍信息defprintIntro():print("22信计1班23号")print("这是单人赛模拟程序:")#获得程序运行参数defgetInputs():a=eval(input("请输入选手A的能力值(0-1):"))b=eval(input("请输入选手B的能力值(0-1):......
  • 体育模拟竞技
    模拟乒乓球比赛importrandom#引用random库defsportgame():print("Welcometothesportgame")print("这个程序将模拟乒乓球比赛")#介绍程序defInputPlayer():player1=eval(input("请输入运动员A能力值:"))player2=eval(input("请输入运动员B能力......
  • mac m1 编译cocos2d-x 在模拟器上运行 一些问题汇总
     如果涉及到侵权请联系本人删除 1  》〉/Users/yzfhkms-m/Library/Developer/Xcode/DerivedData/formi-dlcfwgxcmidqefdkxnvnfwfprpfs/Build/Products/Debug-iphonesimulator/formi-mobile.appisnotavalidpathtoanexecutablefile.Pleaserebuildtheprojectto......
  • 模拟体育分析
     fromrandomimportrandomdefprintInfo():'''function:打印程序的介绍信息'''print("通过输入2个队伍A和B的能力值(0到1之间的小数表示),能够模拟多次2个队伍A和B的排球竞技比赛,从而得出各自的胜率!")print("信计1班罗天智15")defgetInputs():'&#......
  • py123——模拟体育竞技分析:乒乓球比赛
    模拟体育竞技分析:一.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬采用乒乓球比赛规则一局比赛:‪‬......
  • go-使用通道模拟抢到活动
    packagemainimport( "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "math/rand" "sync" "time")//用户结构体typeUserstruct{ Idint Namestring Telstring Addressstring}......
  • 排球比赛模拟
    importrandomdefprint_intro():print("排球比赛模拟程序")print("-------------------------------")definput_teams():team1_ability=float(input("请输入第一个球队的能力值:"))team2_ability=float(input("请输入第二个球队的能力值:"))returnteam1......
  • 【K 个一组翻转链表】模拟
    leetcode25.K个一组翻转链表假设当前需要反转的子链表为[curHead,curTail]curDummy:当前需要反转的子链表的虚拟节点curHead:当前需要反转的子链表的头节点curTail:当前需要反转的子链表的尾节点找到尾节点curTail反转子链表[curHead,curTail](反转子链表解法参考反转子链......
  • 模拟实现strstr函数的代码——《初学C语言第40天》
    //////模拟实现strstr////(字符中的元素是连续存放的,所以不会存在跳动存放的情况,例如a1="ababcd",a2="ac"此结果就是NULL)////情况1.arr1="abcd"arr2="abc"(一次匹配就找到了)////情况2.arr1="ababcd"arr2="abc"(多次匹配才能找到)//#include<stdio.h>//#in......
  • 模拟赛简要题解
    11.16(C0389)100+10+50=160,rk3。本来BC都应该写出来的。A:dp或贪心都可以,贪心直接从下往上覆盖即可。B:注意:这里的\(\oplus\)指的是按位或。合法条件可以化简为:\(\oplus_{i=1}^{p}a_i=\oplus_{i=p+1}^{n}a_i\)。继续挖掘。看到位运算肯定想到拆位,考虑每一位第一次和......