拆分值成团均匀 [英] Splitting values into groups evenly
问题描述
让我尝试解释的情况尽我所能。
Let me try to explain the situation the best I can.
可以说我有3个值
1, 2, 3
我告诉一个算法,这个值分成X列。比方说X = 2澄清。
I tell an algorithm to split this values into x columns. Lets say x = 2 for clarification.
该算法确定该组值中的最好的投入两列以下方式
The algorithm determines that the group of values is best put into two columns the following way.
1st column 2nd column
---------------------------
1 3
2
每个列有偶数个(总计,不是文字)值。
Each column has an even number (totals, not literals) value.
现在可以说我有以下值
7, 8, 3, 1, 4
我告诉大家,我想要的值分为3列的算法。该算法现在告诉我,以下是最合适的。
I tell the algorithm that I want the values split into 3 columns. The algorithm now tells me that the following is the best fit.
1st column 2nd column 3rd column
8 7 3
1 4
注意列怎样的arent安静甚至,但它是接近,因为它可以得到的。多一点,有点下被认为是正常的,只要该列表是接近甚至也可以。
Notice how the columns arent quiet even, but it is as close as it can get. A little over and a little under is considered ok, as long as the list is AS CLOSE TO EVEN AS IT CAN BE.
任何人有什么建议吗?知道这样做有什么好的方法呢?
Anybody got any suggestions? Know any good methods of doing this?
推荐答案
我会做这样的:
- 添加所有值,让我们把这个S
- 将列数除以S,我们称之为米
- 找到的一组值,其中总和是M或尽可能接近至M,采用了背包的算法(例如 http://search.cpan.org/~andale/Algorithm-Knapsack-0.02/lib/Algorithm/Knapsack.pm (只是一个快速谷歌的背包))
- 取设定值的总和,并选自S减去它,姑且称之为吨。
- 将列数减1 鸿沟牛逼
- 和重复算法
- add all the values, let's call this S
- divide S by the number of columns, let's call this M.
- find a set of values of which the sum is M or as close as possible to M, using a knapsack algorithm (e.g. http://search.cpan.org/~andale/Algorithm-Knapsack-0.02/lib/Algorithm/Knapsack.pm (just a quick Google on knapsack))
- take the sum of the set of values and subtract it from S, let's call it T.
- divide T by the number of columns minus 1
- and repeat the algorithm
这篇关于拆分值成团均匀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!