疯狂数队列 编程 python

来源:互联网 发布:json后台传值到前台 编辑:程序博客网 时间:2024/06/07 01:32

思路很简单 就是 先填 最大 然后 左右两年两个最小 然后左右两边再两个次最大 。。。依次进行 

比如 5 10 25 25 40

25 5 40 10 25

具体细节如下

1:先定义两个全局变量 s1 和分别记录 我按照上述规律 填充的list 和 进行 以上规律的次数

对于边界无需迭代的情况 即是t=0

2:mount==1 andt==0

3:mount ==2andt==0

4:mount ==3andt==0

5:mount ==4andt==0

6:mount ==5andt==0

对于我们需要迭代的

在迭代之前

7mount >5 and t==0

需要按照之前的规律 进行先放5个数然后递归        t+=1 return cr(mount -5,s[2:(mount-3)])

因为只要T〉=1

我就每次不需要找最后一个 及时 1 2 3 4 5 6 7 8 9 10

我按照此1 2 10 8 9 还剩下 3 4 5 6 7这个时候 我至于 按照 把 最小 放两边 把最大 放两边 不需要 再找  最大

同理分

8  mount == 1 and t>=1:此时注意 你需要比较abs(s1[0]-s[0])>=abs(s[0]-s1[-1]):

 9  if mount ==2 and t>=1:此时注意 你需要比较s[-1]!=s1[-1]: 聚合例子 1 abcd 20 你最后 还剩 2 20  此时20 2 1 abcd 20   而不是    2 1 abcd 20 20

10   if mount == 3 and t>=1:
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-1])
11    if mount ==4 and t>=1:
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-2])
        s1.insert(len(s1),s[-1])
12    if mount>=5 and t>=1:
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-2])
        s1.insert(len(s1),s[-1])
        t+=1
        return cr(mount -4,s[2:(mount-2)])

    

代码如下  :若各位大神有好的 思路 或者代码 欢迎 指导

def cr(mount,s):

    global s1
    global t
    if mount == 1 and t==0:
        s1.insert(0,s[0])
        return s1
    if mount ==2 and t==0:
        s1.insert(0,s[0])
        s1.insert(len(s1),s[-1])
        return s1
    if mount == 3 and t==0:
        s1.insert(0,s[-1])
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        return s1
    if mount ==4 and t==0:
        s1.insert(0,s[-1])
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-2])
        return s1
    if mount == 5 and t==0:
        s1.insert(0,s[-1])
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-3])
        s1.insert(len(s1),s[-2])
        return s1
    if mount>5 and t==0:
        s1.insert(0,s[-1])
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-3])
        s1.insert(len(s1),s[-2])
        t+=1
        return cr(mount -5,s[2:(mount-3)])
    if mount == 1 and t>=1:
        if abs(s1[0]-s[0])>=abs(s[0]-s1[-1]):
            s1.insert(0,s[0])
        if abs(s1[0]-s[0])<abs(s[0]-s1[-1]):
            s1.insert(len(s1),s[0])
        return s1
    if mount ==2 and t>=1:
        s1.insert(0,s[0])
        if s[-1]!=s1[-1]:
            s1.insert(len(s1),s[-1])
        else:
            s1.insert(0,s[-1])
        return s1
    if mount == 3 and t>=1:
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-1])
        return s1
    if mount ==4 and t>=1:
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-2])
        s1.insert(len(s1),s[-1])
        return s1
    if mount>=5 and t>=1:
        s1.insert(0,s[0])
        s1.insert(len(s1),s[1])
        s1.insert(0,s[-2])
        s1.insert(len(s1),s[-1])
        t+=1
        return cr(mount -4,s[2:(mount-2)])
if __name__ =='__main__':
    mount = int(raw_input())
    s1 = []
    t = 0
    s = raw_input().split(" ")[:mount]
    sum1 = 0
    for i in range(mount):
        s[i] = int(s[i])
    s.sort()
    cr(mount,s)
    for i in range(1,mount):
        sum1 += abs(s1[i]-s1[i-1])
    print sum1
原创粉丝点击