by on 六.11, 2010, under ,
话说拉手网很有钱啊,10天送出55台ipad,具体抽奖规则请见,这个算法可以计算自己中奖机会有多少的。具体如下:
#!/usr/bin/env python #-*-coding: UTF-8-*- import copy pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22] base = 48189L dic = {} def get_pool(origin): ''' 计算22选5的全排列''' result = [[]] length = len(origin) for a in range(0, length): #print 'b1' temp = [] temp.append(origin[a]) for b in range(a+1, length): temp.append(origin[b]) for c in range(b+1, length): temp.append(origin[c]) for d in range(c+1, length): temp.append(origin[d]) for e in range(d+1, length): temp.append(origin[e]) new_temp = copy.copy(temp) result.append(new_temp) #print 'a' temp.remove(origin[e]) temp.remove(origin[d]) temp.remove(origin[c]) temp.remove(origin[b]) temp.remove(origin[a]) return result def calculate(numlist): ''' 计算每种22选5结果组成的整数值''' result = 0L for num in numlist: result = result * 100L + long(num) return result if __name__ == '__main__': resultlist = get_pool(pool) #print resultlist ''' 下面的for循环计算22选5全排列组成整数后,对每天抽奖人数(base)取余后,也就是每种中奖号码的可以获奖的次数''' for numlist in resultlist: current = calculate(numlist) re = str(current % base + 1) if re not in dic: dic[re] = 1 else: dic[re] = dic[re] + 1 #这个是打印所有可能 '''for key, value in dic.items(): print '%s = %d' % (key, value)''' #下面是打印某种可能的概率以及最多可能的次数,还有多少个 result = 0L max = 0 cnt = 0 '''for a in dic.values(): if a > max: max = a if a == 5: cnt = cnt + 1 result = result + a print dic['33828'] * 100.0 / result print max print cnt''' #下面打印特定序号 if '33828' in dic: print '33828 = %d' % dic['33828'] if '47015' in dic: print '47015 = %d' % dic['47015']