个人博客转至:tybai.com

无聊就想打码,打码使我快乐


Fork me on GitHub

python将整数均分成N等分

在python中,需要将整数均分成N等分。python divide integers N equal parts sum

# 拆分整数
def split_integer(m, n):
    assert n > 0
    quotient = int(m / n)
    remainder = m % n
    if remainder > 0:
        return [quotient] * (n - remainder) + [quotient + 1] * remainder
    if remainder < 0:
        return [quotient - 1] * -remainder + [quotient] * (n + remainder)
    return [quotient] * n
x = split_integer(98, 8)
print(x)
[12, 12, 12, 12, 12, 12, 13, 13]

posted on 2018-09-28 11:07 TTyb 阅读(...) 评论(...) 编辑 收藏

导航


不用多久

我就会升职加薪

当上总经理

出任CEO

迎娶白富美

走上人生巅峰

Pulpit rock