📄 ss1.py
字号:
#!/usr/bin/python
#Filename:wendinghunpei.py
def test(nm, nw): # nm and nw stand for new_man and new_woman
stable = 1
if result != []:
for (m, w) in result:
if ((MWR[m][nw] > MWR[m][w]) and (WMR[nw][m] > WMR[nw][nm])) or ((MWR[nm][w] > MWR[nm][nw]) and (WMR[w][nm] > WMR[w][m])) :
stable = 0
break
if stable:
return 1
else:
return 0
def mcp(nm) : # match couple (search wife for man[nm])
for nw in range(0,PPL): #for (nw=0,nw<PPL,nw++)
if lastw[nw] and test(nm, nw) :
lastw[nw] = 0
result.append((nm, nw))
if len(result) < 8 :
mcp(nm+1)
else:
print result
lastw[nw] = 1
result.pop()
# open MWR and WMR files to read
#
inputmwr = open('mwr.dat', 'r')
MWR = inputmwr.readlines()
mppl = len(MWR)
inputwmr = open('wmr.dat', 'r')
WMR = inputwmr.readlines()
wppl = len(WMR)
if mppl != wppl:
print 'The population of men not equals the population of wowen'
exit(1)
# init the population, MWR and WMR
#
PPL = mppl # population
for i in range(0, PPL):
MWR[i] = MWR[i].split()
WMR[i] = WMR[i].split()
lastw = [1]*PPL
result = [] #queue &stack
print 'You have', PPL, 'men and', PPL, 'women to couple, here comes the stable result:'
mcp(0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -