欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

美团2018秋招笔试--向房间分人

程序员文章站 2022-05-26 16:33:38
...
美团2018秋招笔试--向房间分人
美团2018秋招笔试--向房间分人


当时没写出来,现在后头写一下  哎。。。其实没那么难。上代码!
import numpy as np
import copy
def get_clclist(allo_ed):
    min_val = min(allo_ed)
    return range(min_val + 1)

def people_alloc(n, x, allo_ed):
    for i in range(1, n + 1):
        list_clc = get_clclist(allo_ed)
        for clc in list_clc:
            if clc == 0 and  x > i:
                num = x - i
                allo_pre = copy.deepcopy(allo_ed)
                for index in range(i + 1, x + 1):
                    allo_pre[index - 1] = allo_pre[index - 1] - 1
                allo_pre[i - 1] = num
                if allo_ed[i - 1] == clc:
                    return allo_pre
            elif clc == 0 and x < i:
                num = n - (i - x)
                allo_pre = copy.deepcopy(allo_ed)
                for index in range(1, x + 1):
                    allo_pre[index - 1] = allo_pre[index - 1] - 1
                for index in range(i + 1, n + 1):
                    allo_pre[index - 1] = allo_pre[index - 1] - 1
                allo_pre[i - 1] = num
                if allo_ed[i - 1] == clc:
                    return allo_pre
            elif clc != 0 and  x >= i:
                num = clc * n + x - i
                allo_pre = copy.deepcopy(allo_ed)
                for index in range(i + 1, x + i):
                    allo_pre[index - 1] = allo_pre[index - 1] - 1
                for index in range(1, n + 1):
                    allo_pre[index - 1] = allo_pre[index - 1] - clc
                if allo_ed[i - 1] == clc:
                    return allo_pre
            elif clc != 0 and  x < i:
                num = n - (i - x) + clc * n
                allo_pre = copy.deepcopy(allo_ed)
                for index in range(1, x + 1):
                    allo_pre[index - 1] = allo_pre[index - 1] - 1
                for index in range(i + 1, n + 1):
                    allo_pre[index - 1] = allo_pre[index - 1] - 1
                for index in range(1, n + 1):
                    allo_pre[index - 1] = allo_pre[index - 1] - clc
                allo_pre[i - 1] = num
                if allo_ed[i - 1] == clc:
                    return allo_pre


if __name__ == "__main__":
    '''
    str = raw_input("input n and x:")
    str = str.strip("\n")
    str_spl = str.split(" ")
    n = int(str_spl[0])
    x = int(str_spl[1])
    '''

    n, x = raw_input("input n and x:").strip().split()
    print(n)
    n = int(n)
    x = int(x)

    '''
    input_people = raw_input("input people num in every room:")
    input_list = input_people.split(" ")
    num = []
    for i in range(n):
        num.append(int(input_list[i]))
    '''

    num = [int(i) for i in raw_input("input people num in every room:").strip().split()]
    #注意要使用raw_input()而不能是input()

    result = people_alloc(n, x, num)
    print result







相关标签: 美图笔试 python