Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28403
  • 博文数量: 10
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-14 11:23
文章分类

全部博文(10)

文章存档

2015年(2)

2014年(8)

我的朋友

分类: Python/Ruby

2014-12-12 15:09:28

以下是自己在最近闲来无事计算彩票中奖概率时用到的一些函数:


点击(此处)折叠或打开

  1. import operator
  2. import itertools
  3. import sys


  4. # 组合函数
  5. def C(n, k):
  6.     return reduce(operator.mul, range(n - k + 1, n + 1)) / reduce(operator.mul, range(1, k + 1))


  7. # 购买组合复式的花费
  8. def spendMoney(x, x1, y, y1, additional):
  9.     if additional:
  10.         return C(x, x1) * C(y, y1) * 3
  11.     else:
  12.         return C(x, x1) * C(y, y1) * 2


  13. # 输出组合元组的列表
  14. def combination(listData, num):
  15.     return list(itertools.combinations(listData, num))


  16. # 重定向输出
  17. def changeOut():
  18.     oldStdout = None
  19.     logfile = None
  20.     # try:
  21.     logfile = open('lotteryResult.txt', 'w+')
  22.     oldStdout = sys.stdout
  23.     sys.stdout = logfile
  24.     # finally:
  25.     # if logfile:
  26.     # logfile.close()
  27.     # if oldStdout:
  28.     # sys.stdout = oldStdout



阅读(541) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~