美团2018秋招笔试--向房间分人
程序员文章站
2022-05-26 16:33:38
...
当时没写出来,现在后头写一下 哎。。。其实没那么难。上代码!
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
下一篇: 元恂为什么没有娶容止的孙女?幸好没娶成
推荐阅读