知乎看到的一道题:
手里有一副从1到n的牌,每次从牌堆顶取一张放桌子上,再取一张放牌堆底,直到手里没牌。这时候,桌子上的牌是从1到n有序的。设计程序,输入n,输出牌堆的顺序数组。
[1 2 3 4 5]
[1] [2 3 4 5]
[1] [3 4 5 2]
[1 3] [4 5 2]
[1 3] [5 2 4]
[1 3 5] [2 4]
[1 3 5] [4 2]
[1 3 5 4] [2]
[1 3 5 4] [2]
[1 3 5 4 2]
原数组 vs 变换后:
[1 2 3 4 5]
[1 3 5 4 2]
那么,现数组元素下标到原数组下标的映射关系:
左侧为元素在现数组的下标;
右侧为元素在原数组的下标。
1 -- 1
2 -- 3
3 -- 5
4 -- 4
5 -- 2
即原数组为x,现数组为y,则:
x[1] = y[1]
x[3] = y[2]
x[5] = y[3]
x[4] = y[4]
x[2] = y[5]
#coding: utf-8
def mi_test(n):
hand_list = list(range(1, n+1))
table_list = []
hand_list_copy = hand_list[:]
while(hand_list_copy):
table_list.append(hand_list_copy.pop(0))
if hand_list_copy:
hand_list_copy.append(hand_list_copy.pop(0))
list_map = {}
for i in range(n):
list_map[i] = table_list.index(hand_list[i])
result_list = []
table_list = hand_list
for i in range(n):
result_list.append(table_list[list_map.get(i)])
return result_list
def mi_test2(n):
hand_list = list(range(1, n + 1))
table_list = []
hand_list_copy = hand_list[:]
while (hand_list_copy):
table_list.append(hand_list_copy.pop(0))
if hand_list_copy:
hand_list_copy.append(hand_list_copy.pop(0))
for i in range(n):
hand_list[table_list[i] - 1] = i + 1
return hand_list
print(mi_test(5))
# [1, 5, 2, 4, 3]
print(mi_test2(5))
# [1, 5, 2, 4, 3]