def success():
p = 1/5,1/3,1/4
t = 1
for i in p:
t *= 1-i
return 1-t
print(f'成功概率:{success():.3f}')
p1 = [1-0.4,0.4] # 不中和击种的概率
p2 = [0,0.2,0.6,1.0] # 不同人数击中的坠机概率
p0 = 0 # 初始总概率
for i in range(2):
for j in range(2):
for k in range(2):
t = p1[i] * p1[j] * p1[k] * p2[i+j+k]
p0 += t
print(f'{i} {j} {k} : {p1[i]} * {p1[j]} * {p1[k]} * {p2[i+j+k]:.1f} = {t:.4f}')
import fractions
print(f'\nTotal : {fractions.Fraction(str(0.3232))}')
'''
0 0 0 : 0.6 * 0.6 * 0.6 * 0.0 = 0.0000
0 0 1 : 0.6 * 0.6 * 0.4 * 0.2 = 0.0288
0 1 0 : 0.6 * 0.4 * 0.6 * 0.2 = 0.0288
0 1 1 : 0.6 * 0.4 * 0.4 * 0.6 = 0.0576
1 0 0 : 0.4 * 0.6 * 0.6 * 0.2 = 0.0288
1 0 1 : 0.4 * 0.6 * 0.4 * 0.6 = 0.0576
1 1 0 : 0.4 * 0.4 * 0.6 * 0.6 = 0.0576
1 1 1 : 0.4 * 0.4 * 0.4 * 1.0 = 0.0640
Total : 202/625
'''
继上题,只是3人的命中率不同;代码稍作修改即可:
p1 = [[1-0.4,0.4],[1-0.5,0.5],[1-0.7,0.7]]
p2 = [0,0.2,0.6,1.0] # 不同人数击中的坠机概率
p0 = 0 # 初始总概率
for i in range(2):
for j in range(2):
for k in range(2):
t = p1[0][i] * p1[1][j] * p1[2][k] * p2[i+j+k]
p0 += t
print(f'{i} {j} {k} : {p1[0][i]:.1f} * {p1[1][j]:.1f} * {p1[2][k]:.1f} * {p2[i+j+k]:.1f} = {t:.4f}')
import fractions
print(f'\nTotal : {fractions.Fraction(str(round(p0,5)))}')
'''
0 0 0 : 0.6 * 0.5 * 0.3 * 0.0 = 0.0000
0 0 1 : 0.6 * 0.5 * 0.7 * 0.2 = 0.0420
0 1 0 : 0.6 * 0.5 * 0.3 * 0.2 = 0.0180
0 1 1 : 0.6 * 0.5 * 0.7 * 0.6 = 0.1260
1 0 0 : 0.4 * 0.5 * 0.3 * 0.2 = 0.0120
1 0 1 : 0.4 * 0.5 * 0.7 * 0.6 = 0.0840
1 1 0 : 0.4 * 0.5 * 0.3 * 0.6 = 0.0360
1 1 1 : 0.4 * 0.5 * 0.7 * 1.0 = 0.1400
Total : 229/500
'''
小数转分数(以下基本是为了凑字数,不喜勿喷忽略即可)
| Examples
| --------
|
| >>> Fraction(10, -8)
| Fraction(-5, 4)
| >>> Fraction(Fraction(1, 7), 5)
| Fraction(1, 35)
| >>> Fraction(Fraction(1, 7), Fraction(2, 3))
| Fraction(3, 14)
| >>> Fraction('314')
| Fraction(314, 1)
| >>> Fraction('-35/4')
| Fraction(-35, 4)
| >>> Fraction('3.1415') # conversion from numeric string
| Fraction(6283, 2000)
| >>> Fraction('-47e-2') # string may include a decimal exponent
| Fraction(-47, 100)
| >>> Fraction(1.47) # direct construction from float (exact conversion)
| Fraction(6620291452234629, 4503599627370496)
| >>> Fraction(2.25)
| Fraction(9, 4)
| >>> Fraction(Decimal('1.47'))
| Fraction(147, 100)
| >>> Fraction('8.125')
| Fraction(65, 8)
| >>> print(Fraction('8.125'))
| 65/8
| >>> print(Fraction(0.125))
| 1/8
另外解决概率题经常要用到排列、组合函数:
Help on class combinations in module itertools:
class combinations(builtins.object)
| combinations(iterable, r)
|
| Return successive r-length combinations of elements in the iterable.
|
| combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
|
| Methods defined here:
|
| getattribute(self, name, /)
| Return getattr(self, name).
|
| iter(self, /)
| Implement iter(self).
|
| next(self, /)
| Implement next(self).
|
| reduce(...)
| Return state information for pickling.
|
| setstate(...)
| Set state information for unpickling.
|
| sizeof(...)
| Returns size in memory, in bytes.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| new(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
Help on class permutations in module itertools:
class permutations(builtins.object)
| permutations(iterable, r=None)
|
| Return successive r-length permutations of elements in the iterable.
|
| permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
|
| Methods defined here:
|
| getattribute(self, name, /)
| Return getattr(self, name).
|
| iter(self, /)
| Implement iter(self).
|
| next(self, /)
| Implement next(self).
|
| reduce(...)
| Return state information for pickling.
|
| setstate(...)
| Set state information for unpickling.
|
| sizeof(...)
| Returns size in memory, in bytes.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| new(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
还有一个可以取重复值的组合公式,知道的比较少:
Help on class combinations_with_replacement in module itertools:
class combinations_with_replacement(builtins.object)
| combinations_with_replacement(iterable, r)
|
| Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.
|
| combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"
|
| Methods defined here:
|
| getattribute(self, name, /)
| Return getattr(self, name).
|
| iter(self, /)
| Implement iter(self).
|
| next(self, /)
| Implement next(self).
|
| reduce(...)
| Return state information for pickling.
|
| setstate(...)
| Set state information for unpickling.
|
| sizeof(...)
| Returns size in memory, in bytes.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| new(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://hannyang.blog.csdn.net/article/details/125078168
内容来源于网络,如有侵权,请联系作者删除!