PHP算法将数组中的项目分组,以便组的总和等于可被8整除的数字

时间:2017-04-27 15:49:15

标签: php algorithm

我现在已经解决了这个问题一段时间了,而我还不确定如何解决它。我的数组看起来像这样,但如果它更容易解决,我愿意重新格式化:

Array
(
[0] => Array
    (
        [name] => ff
        [number_bits] => 10
        [description] => asdf
    )

[1] => Array
    (
        [name] => hh
        [number_bits] => 5
        [description] => asdf
    )

[2] => Array
    (
        [name] => dd
        [number_bits] => 4
        [description] => asdf
    )

[3] => Array
    (
        [name] => ee
        [number_bits] => 4
        [description] => asfd
    )

[4] => Array
    (
        [name] => gg
        [number_bits] => 4
        [description] => asdf
    )

[5] => Array
    (
        [name] => bb
        [number_bits] => 2
        [description] => asdf
    )

[6] => Array
    (
        [name] => aa
        [number_bits] => 1
        [description] => asdf
    )

[7] => Array
    (
        [name] => cc
        [number_bits] => 1
        [description] => sdaf
    )

[8] => Array
    (
        [name] => ii
        [number_bits] => 1
        [description] => asdf
    )

[9] => Array
    (
        [name] => jj
        [number_bits] => 1
        [description] => afsd
    )

)

我需要做的是将这些项目组合在一起,以便' number_bits'加起来可被8整除的数字。例如,通过查看这些,我可以看到ff,hh,& aa可以组合在一起,因为每个的number_bits加起来为16个。另外,dd和ee可以组合在一起,因为它们加起来为8.我不希望超出最多24个的组,如果这使它更容易。

0 个答案:

没有答案