小编Sed*_*Sed的帖子

将数字分成(几乎)相等的整数的算法

我有一种情况,我有发票电子表格传入,单行跨越多个月份,数量列包含跨越所有月份的数量总和。

为了运行逐月分析,我们需要将总数量拆分为跨 n 行的相等(ish)数量,其中 n 是跨越的月数。

这些数字可能相差一两个,但每个元素之间的差异越小越好。

我有一个我在 python 中做的粗略模型,但我觉得有一种更好的方法可以做到这一点。注意:请原谅......一切:

from __future__ import division
import math
def evenDivide(num, div):
    splits = []
    sNum = str(num/div)
    remainder = float(sNum[sNum.index('.'):])
    #print "Remainder is " + str(remainder)
    integer = math.floor(num/div)
    #print "Integer is " + str(integer)
    totRemainder = round(remainder * div, 2)
    #print "Total Remainder is " + str(totRemainder)
    for index in range(div):
        if (totRemainder > 0):
            totRemainder -= 1 if (index%2 == 0) else 0
            if (index % 2 == 0):
                splits.append(int(integer …
Run Code Online (Sandbox Code Playgroud)

python algorithm math division

7
推荐指数
2
解决办法
7006
查看次数

标签 统计

algorithm ×1

division ×1

math ×1

python ×1