Skip to content

Instantly share code, notes, and snippets.

@Yhzhtk
Last active December 19, 2015 02:29
Show Gist options
  • Save Yhzhtk/5883847 to your computer and use it in GitHub Desktop.
Save Yhzhtk/5883847 to your computer and use it in GitHub Desktop.
三个鬼和三个人过河,初始都在一侧,要到另一侧。现在只有一个船,一次做多乘坐两个(人人、人鬼或鬼鬼)。任何时刻任何一侧的鬼的数量多于人,则鬼会把人吃掉,现在要给出一种方案,让鬼和人都过到河的另一侧。未完善。。
# coding=utf-8
'''
Created on 2014-4-27
@author: delfi@lagou.com
'''
def calc_base(base):
'''calculate the numbers meet the base conditions, l1 and l2 is sorted, then merge sort without repeat'''
# 0 special
if base == 0:
ll = [i * 10 for i in range(1, 11)]
return ll
# contain base
l1 = []
bsingle = base
bten = base * 10
for i in range(1, 10):
bsingle += 10
if i != base:
l1.append(bsingle)
else:
l1.extend([bten + j for j in range(10)])
# multiple
l2 = []
t = 100 / base + 1
bmulti = 0
for i in range(1, t):
# no use multiplication, use addition for efficient
bmulti += base
l2.append(bmulti)
# merge sort, not repeat
ll = []
while l1 and l2:
if l1[0] < l2[0]:
ll.append(l1.pop(0))
elif l1[0] > l2[0]:
ll.append(l2.pop(0))
else:
# for not repeat
l2.pop(0)
return ll + l1 + l2
def match_condition(n, ll, say, result):
'''judge whether match the list'''
for l in ll:
if n == l:
result[0] = True
result[1] = result[1] + say
break
elif n < l:
# because ll is sorted so it can break
break
return result
def test_calc():
'''test the calc_base method'''
for i in range(10):
print i, len(calc_base(i))
print calc_base(i)
print
if __name__ == '__main__':
'''I calculate the list by addition rather than use for i in range 100 by division,
I think the addition is more efficient than division even thought it's like more complex'''
n1 = int(raw_input("Input the first single digits:"))
n2 = int(raw_input("Input the second single digits:"))
n3 = int(raw_input("Input the third single digits:"))
lf = calc_base(n1)
lb = calc_base(n2)
lw = calc_base(n3)
for n in xrange(1, 101):
result = [False, '']
match_condition(n, lf, 'Fizz', result)
match_condition(n, lb, 'Buzz', result)
match_condition(n, lw, 'Whizz', result)
if result[0]:
print result[1]
else:
print n
#coding=utf-8
'''
三个鬼和三个人过河,初始都在一侧,要到另一侧。
现在只有一个船,一次做多乘坐两个(人人、人鬼或鬼鬼)。
任何时刻任何一侧的鬼的数量多于人,则鬼会把人吃掉,现在要给出一种方案,让鬼和人都过到河的另一侧。
Created on 2013-6-28
@author: gdh
'''
def judge(list):
'''判断是否被吃'''
l = len(list)
gc = 0
for c in list:
if c == 'g':
gc += 1
if gc > l - gc:
return False
return True
def success(left, right):
'''判断是否完成目标'''
if len(left) == 6 and len(right) == 0:
return True
return False
def contain(states, left):
'''判断是否走了重复路'''
for state in states:
if len(state) == len(left):
for c in state:
if c not in left:
return False
return True
return False
def back(states, left, right):
left = states[len(states) - 2]
left = states[len(states) - 2]
def next(states, left, right):
'''继续下一步'''
for i in left:
nleft = left[0, 0]
nright = right[0, 0]
nright.append(nleft[i])
del nleft[i]
if not(judge(nleft) and judge(nright)):
pass
if __name__ == '__main__':
left = []
states =[left]
right = ['r', 'r', 'r', 'g', 'g', 'g']
while not success(left, right) :
next(states, left, right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment