美团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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值