Skip to content

Instantly share code, notes, and snippets.

@alex-1q84
Last active December 16, 2015 05:59
Show Gist options
  • Save alex-1q84/5388702 to your computer and use it in GitHub Desktop.
Save alex-1q84/5388702 to your computer and use it in GitHub Desktop.
未婚妻问题 关于女神选择白马王子的最佳方案和常数e的关系 see http://www.guokr.com/article/6768/
#!/usr/bin/env python
#-*-coding: UTF-8-*-
'''未婚妻问题
关于女神选择白马王子的最佳方案和常数e的关系
see http://www.guokr.com/article/6768/
'''
import random
import math
will_meet_whitehorses = 30
married = {}
for i in range(1, will_meet_whitehorses+1):
married[i] = 0
to_be_leaved = int(round(will_meet_whitehorses / math.e))
for i in xrange(1, 10001):
whitehorses = range(1, will_meet_whitehorses+1)
random.shuffle(whitehorses)
best_in_been_leaved = max(whitehorses[0: to_be_leaved])
for n in whitehorses[to_be_leaved: ]:
if n > best_in_been_leaved:
married[n] = married[n] + 1
break
else:
married[n] = married[n] + 1
for i in range(1, will_meet_whitehorses+1):
print '%d\t\t%d' % (i, married[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment