简体   繁体   English

益智游戏:如何按特定列的特定总和对行进行分组

[英]Puzzler: How To Group Rows By A Particular Sum of a Particular Column

How to burst the following rows into 3 groups where the sum of "dollars" is 10. All rows must be used and none more than once. 如何将后面的行分成3组,其中“美元”的总和为10。必须使用所有行,且不得超过一次。

row|dollars
1|1
2|1
3|1
4|1
5|1
6|1
7|3
8|4
9|7
10|10

One (of many possible) desired outcomes would be... (可能的)预期结果之一是...

Row Group 1 = 10
Row Group 2 = 7,9
Row Group 3 = 1,2,3,4,5,6,8

Extra Credit: When it's not mathematically possible to get a sum of exactly $10 in each group, is there a formula for getting us closest to that? 额外积分:如果在数学上不可能每组都只得到10美元,那么是否有公式可以使我们与之最接近

I though maybe " HAVING sum(dollar) = 10 " or, for a close solution, just sorting and distributing one row to one group, but that didn't get me close. 我虽然也许是“ HAVING sum(dollar)= 10”,或者,对于一个紧密的解决方案,我只是将一行排序并分配给一组,但这并不能使我接近。

Group Row By Sum of Specific Column equal to Specific Value sort of touches on this, but, assuming their could be millions of rows, there would be performance issues. 将“按特定列的总和分组行”等于“特定值” ,在某种意义上是这样,但是,假设它们可能是数百万行,则将存在性能问题。 I'm stumped. 我很沮丧

If it helps, I'm using php and mysql. 如果有帮助,我正在使用php和mysql。 A sql-only solution would be ideal, but some combination would be great too. 仅使用sql的解决方案将是理想的选择,但是某些组合也将是很好的选择。 Thanks for any suggestions. 感谢您的任何建议。

Edit: If I wasn't clear, I want to return the rows that make this possible, not just "group by" them. 编辑:如果我不清楚,我想返回使之成为可能的行,而不仅仅是“分组”。

I don't think you could do it with just sql, but it could certainly help out. 我认为您不能仅使用sql就能做到,但肯定可以帮上忙。 I'd start by having a query like select * from data where dollars <= 10 order by dollars desc and then make an algorithm in php that went through the results and added up dollars until it found three sets that add up to 10. 我首先要进行如下查询: select * from data where dollars <= 10 order by dollars desc ,然后在php中制作一个算法,遍历结果并将其加起来,直到找到三个加起来等于10的集合。

It would start out adding from larger to smaller until it found a correct sum, then store it, remove those items from the list and start again, three times. 它将开始从大到小相加,直到找到正确的总和,然后将其存储,从列表中删除这些项目,然后再次开始三次。 I'm on my phone, but will update the answer with a working example when I get to my computer. 我正在用手机,但是当我使用计算机时,将通过一个有效的示例来更新答案。

EDIT: got to my computer. 编辑:到我的电脑。 This is very messy code, but it should lead you in the right direction. 这是非常混乱的代码,但是它应该引导您正确的方向。

$dataset = [10,7,4,3,1,1,1,1,1,1];
$results = [];

function add_to_ten(&$data) {
    $sum = 0;
    $results = [];

    foreach ($data as $index => &$datum) {
        if ($datum == 0) {
            continue;
        }
        if ($sum + $datum <= 10) {
            $sum += $datum;
            $results[] = $index;
            $datum = 0;
        }
        if ($sum == 10) {
            return $results;
        }
    }
}

print_r(add_to_ten($dataset));
print_r(add_to_ten($dataset));
print_r(add_to_ten($dataset));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM