一个面试遇到的题
# -*-encoding:utf-8-*- import sys
def jump(journey, walked=None, counter=None):
if walked is None:
walked = []
if counter is None:
counter = []
for step in [1, 2]:
walking = walked + [step]
if sum(walking) >= journey:
print(walking)
counter.append(1)
continue
jump(journey, walking, counter)
return len(counter)
def main(journey):
print(jump(journey))
if __name__ == "__main__":
main(5)
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 2]
[1, 1, 1, 2]
[1, 1, 2, 1]
[1, 1, 2, 2]
[1, 2, 1, 1]
[1, 2, 1, 2]
[1, 2, 2]
[2, 1, 1, 1]
[2, 1, 1, 2]
[2, 1, 2]
[2, 2, 1]
[2, 2, 2]
13
这个题主要就是考算法-递归