It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
我正在想办法解决这个问题:
生成10个点之间的三角形的所有可能性,这些点的位置如下:

.   .   .

.   .   .

.   .   .

    .

(就像普通手机上的小键盘)
第一行:123
二线:4 5 6
第三行:7 8 9
第四行:0
我正在使用php。我试图使用oposite逻辑生成(获取所有组合并移除所有不是三角形的组合,但这太复杂了)。有人有什么建议吗?

最佳答案

我想到了以下几点:

$banned = array(5,8,10);
$array = array();
for($i=1;$i<11;$i++){
    for($j=1;$j<11;$j++){
        for($k=1;$k<11;$k++){
            if($i != $j && $j != $k && $i != $k){
                $tmp = array($i, $j, $k);
                sort($tmp);
                if($tmp[1]-$tmp[0] !== $tmp[2]-$tmp[1] AND $tmp !== $banned){
                    $array[] = $tmp;
                }
            }
        }
    }
}
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));// remove duplicates
print_r($array); // printing

关于php - 如何从10个可能的点生成三角形的所有可能性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16994281/

10-12 07:36
查看更多