小米面试题目: 一副从1到n的牌,每次从牌堆顶取一张放桌子上,再取一张放牌堆底,直到手中没牌,最后桌子上的牌是从1到n有序,设计程序,输入n,输出牌堆的顺序数组

题目:一副从1到n的牌,每次从牌堆顶取一张放桌子上,再取一张放牌堆底,直到手中没牌,最后桌子上的牌是从1到n有序,设计程序,输入n,输出牌堆的顺序数组

之前这道题很火,所以我也试着写了答案,今天才发出来。结果百度了一个题目的名字,还发现了Java版本的,我在写之前没有看任何人的东西,骗人是小狗。

经本人测试,算法处理木有问题。


from utils.result_process import success, aborted
import re
import copy


async def ls(app, request):

    test_list = [1,2,3,4,5,6]
    test_list_other = copy.deepcopy(test_list)
    n = 5
    # test_list = [x for x in list(range(n))]
    print('--------==========')
    print(test_list)
    other_List = []

    sign = True
    for i in range(len(test_list)+20):
        if test_list:
            if sign == False:
                test_list.append(test_list[0])
                test_list.remove(test_list[0])
                print('----=')
                print(test_list)
                sign = True
            elif sign == True:
                other_List.append(test_list[0])
                test_list.remove(test_list[0])
                print('-----')
                print(test_list)
                sign = False

    print('-=-=-=-=-=-=-=-=-=-=-=-=-')
    print(test_list)
    print(other_List)

    res_dict = {}
    res_list = []
    print('-=-=-=-test_list_other=-=-=-=-')
    print(test_list_other)
    for key, val in enumerate(test_list_other):
        for keyl, item in enumerate(other_List):
            if keyl == key:
                res_dict[item] = val

    print(res_dict)

    for i in range(len(test_list_other)):
        res_list.append(res_dict[i+1])
    print(res_list)
    return success()

def data_judge(data):
    if data % 2 == 0:
        return 'oushu'
    elif data % 2 == 1:
        return 'jishu'

Java版本在这里

https://blog.csdn.net/weixin_42038771/article/details/81879300

 

 

 

点赞