2019-05-12 Python之模拟体育竞赛
一.简介
可以选择任意规则,模拟不同的两个队伍进行球赛的模拟比赛
二.源代码
函数介绍:
from random import * #输出介绍信息
def printIntro():
print("这个程序模拟两个选手A和B的某种竞技比赛")
print("程序运行需要A和B的能力值(以0到1之间的小数表示)") #输入,获取能力值和比赛场数
def getInputs():
#获取数据 a = 0.45
b = 0.5
n = 5
'''
a = eval(input("请输入a选手的能力值:"))
b = eval(input("请输入b选手的能力值:"))
n = eval(input("模拟比赛场次:"))
'''
return a,b,n #比赛全过程
def simNGames(n,probA,probB):
print("竞技分析开始,共模拟{}场比赛".format(n))
winsA, winsB = 0,0
m =eval(input("1.排球,2乒乓球,3足球,4篮球,请输入选择的规则:")) for i in range(1, int(n/2)+2): #n为比赛场数
scoreA, scoreB=simOneGame(int(n/2)+1, probA, probB, m) #单场比赛过程
print("第{}场比赛得分情况,A:{},B:{}".format(i, scoreA, scoreB))
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB #单场比赛过程,返回比分
def simOneGame(i, probA, probB, m):
global k #记录局数
k += 1
if m == 1 :
scoreA, scoreB = Volleyball_game(k, i, probA, probB)
elif m == 2:
scoreA, scoreB = Table_Tennis_game(k, i, probA, probB)
elif m == 3:
scoreA, scoreB = Football_game(i, probA, probB)
elif m == 4:
scoreA, scoreB = Basketball_game(i, probA, probB) return scoreA, scoreB #输出获胜场数和比例
def printSummary(winsA, winsB):
n = winsA + winsB
print("选手A获胜{}场比赛,占比例{:0.2%}".format(winsA, winsA/n))
print("选手B获胜{}场比赛,占比例{:0.2%}".format(winsB, winsB/n)) #排球比赛单局
def Volleyball_game(k, i, probA, probB):
scoreA, scoreB = 0,0
serving = 'A'
if k!=i: #不是最终局
while not ((scoreA == 25 and scoreA >= scoreB-2) or (scoreB == 25 and scoreB >= scoreA-2)) : #如果未达到单场结束条件
if serving == 'A':
if random()<probA:
scoreA += 1
else:
serving='B'
else:
if random()<probB:
scoreB += 1
else:
serving='A'
else: #最终局
while not ((scoreA == 15 and scoreA >= scoreB-2) or (scoreB == 15 and scoreB >= scoreA-2)):
if serving == 'A':
if random()<probA:
scoreA += 1
else:
serving='B'
else:
if random()<probB:
scoreB += 1
else:
serving='A'
return scoreA, scoreB #足球比赛一场
def Football_game(i, probA, probB):
scoreA, scoreB = 0, 0
a, b = randint(int(probA*7), int(probA*14)), randint(int(probB*7), int(probB*14)) #根据能力值随机出进攻次数
for i in range(int(a)):
if random() - 0.1 > probA:
scoreA += 1
for i in range(int(b)):
if random() - 0.1 > probB:
scoreB += 1 while scoreA == scoreB : #点球大战
if random() > probA:
scoreA += 1
if random() > scoreB :
scoreB += 1
return scoreA, scoreB #乒乓球比赛单局
def Table_Tennis_game(k, i, probA, probB):
scoreA, scoreB = 0, 0
serving = 'A'
while not ((scoreA == 11 and scoreA >= scoreB-2) or (scoreB == 11 and scoreB >= scoreA-2)): #如果未达到单场结束条件
if serving == 'A':
if random() < probA:
scoreA += 1
else:
serving = 'B'
else:
if random() < probB:
scoreB += 1
else:
serving = 'A'
return scoreA, scoreB #篮球比赛一场
def Basketball_game(k, i, probA, probB):
scoreA, scoreB = 0, 0
a, b = randint(int(probA * 150), int(probA * 170)), randint(int(probB * 150), int(probB * 170)) # 根据能力值随机出进攻次数
for i in range(int(a)):
if random() > probA:
if random() > 0.62:
scoreA += 3
else:
scoreA += 2
for i in range(int(b)):
if random() > probB:
if random() > 0.62:
scoreB += 3
else:
scoreB += 2 while scoreA == scoreB: # 加时赛
a, b = randint(int(probA * 10), int(probA * 15)), randint(int(probB * 10), int(probB * 15)) # 根据能力值随机出进攻次数
for i in range(int(a)):
if random() > probA:
if random() > 0.62:
scoreA += 3
else:
scoreA += 2
for i in range(int(b)):
if random() > probB:
if random() > 0.62:
scoreB += 3
else:
scoreB += 2
return scoreA, scoreB def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB) k = 0
main()
三.效果
2019-05-12 Python之模拟体育竞赛的更多相关文章
- python初体验 ——>>> 模拟体育竞技
python初体验 ——>>> 模拟体育竞技 一.排球训练营 1. 简介: 模拟不同的两个队伍进行排球的模拟比赛. 2. 模拟原理: 通过输入各自的能力值(Ⅰ),模拟比赛的进行( P ...
- 2019.8.3 [HZOI]NOIP模拟测试12 C. 分组
2019.8.3 [HZOI]NOIP模拟测试12 C. 分组 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 刚看这题觉得很难,于是数据点分治 k只有1和2两种,分别 ...
- 2019.8.3 [HZOI]NOIP模拟测试12 B. 数颜色
2019.8.3 [HZOI]NOIP模拟测试12 B. 数颜色 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 数据结构学傻的做法: 对每种颜色开动态开点线段树直接维 ...
- 2019.8.3 [HZOI]NOIP模拟测试12 A. 斐波那契(fibonacci)
2019.8.3 [HZOI]NOIP模拟测试12 A. 斐波那契(fibonacci) 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 找规律 找两个节点的lca,需 ...
- 7.搭建hyperledger fabric环境及启动——2019年12月12日
2019年12月12日13:05:16 声明:从网络中学习整理实践而来. 1.介绍fabric Fabric 是一个面向企业应用的区块链框架,基于 Fabric 的开发可以粗略分为几个层面: 1. 参 ...
- AHKManager.ahk AHK管理器 2019年12月15日
AHKManager.ahk AHK管理器 2019年12月15日 快捷键 {Alt} + {F1} ///////////////////////////////////////////// ...
- 用python实现模拟登录人人网
用python实现模拟登录人人网 字数4068 阅读1762 评论19 喜欢46 我决定从头说起.懂的人可以快速略过前面理论看最后几张图. web基础知识 从OSI参考模型(从低到高:物理层,数据链路 ...
- python selenium5 模拟点击+拖动+按照指定相对坐标拖动 58同城验证码
#!/usr/bin/python # -*- coding: UTF-8 -*- # @Time : 2019年12月9日11:41:08 # @Author : shenghao/10347899 ...
- 36.React基础介绍——2019年12月24日
2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...
随机推荐
- ModelForm理解简单运用(增删改查)
from django.shortcuts import render, redirect,HttpResponse# Create your views here.from django.forms ...
- spring官方demo及配置查看
1.http://spring.io/projects/spring-framework 2.https://github.com/spring-projects/spring-mvc-showcas ...
- 基于Modbus三种CRC16校验方法的性能对比
目录 1.背景介绍 2. CRC校验的三种方法 2.1. 直接计算CRC校验 2.2. 查短表法计算CRC16校验 2.3.查大表法计算CRC16校验 3.三种校验方式的测试方法 3.1.直接计算CR ...
- [Redis] 万字长文带你总结Redis,助你面试升级打怪
文章目录 Redis的介绍.优缺点.使用场景 Linux中的安装 常用命令 Redis各个数据类型及其使用场景 Redis字符串(String) Redis哈希(Hash) Redis列表(List) ...
- Building Applications with Force.com and VisualForce (DEV401) (二十):Visualforce Pages: Visualforce Componets (Tags)
Dev401-021:Visualforce Pages: Visualforce Componets (Tags) Module Agenda1.Tag Basics2.Tag Bindings T ...
- 怎样设计最优的卷积神经网络架构?| NAS原理剖析
虽然,深度学习在近几年发展迅速.但是,关于如何才能设计出最优的卷积神经网络架构这个问题仍在处于探索阶段. 其中一大部分原因是因为当前那些取得成功的神经网络的架构设计原理仍然是一个黑盒.虽然我们有着关于 ...
- coding++ :JS-判断当前是否是IE浏览器,并返回时IE几?
IEVersion(); function IEVersion() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var is ...
- Spring - 事务管理概述
什么是事务管理? 第一个问题:什么是事务? 事务一般是相对数据库而言的,对于数据库一次操作就属于一个事务, 一次操作可以是几句 SQL 语句,也可以是若干行 JDBC 的 Java 语句.事务既然 ...
- 如何测试Linux命令运行时间?
良许在工作中,写过一个 Shell 脚本,这个脚本可以从 4 个 NTP 服务器轮流获取时间,然后将最可靠的时间设置为系统时间. 因为我们对于时间的要求比较高,需要在短时间内就获取到正确的时间.所以我 ...
- Kaggle竞赛入门(二):如何验证机器学习模型
本文翻译自kaggle learn,也就是kaggle官方最快入门kaggle竞赛的教程,强调python编程实践和数学思想(而没有涉及数学细节),笔者在不影响算法和程序理解的基础上删除了一些不必要的 ...