如何在java或php中编写查找概率的代码
我想制作一个如下示例所示的程序如何在java或php中编写查找概率的代码,java,php,probability,Java,Php,Probability,我想制作一个如下示例所示的程序 input : 2 output : (1) , (2); (1,2), (2,2); input : 3 output : (1),(2),(3); (1,2),(1,3),(2,3); (1,2,3) 公式为2^n-1 我只试过用一次。我想得到所有的可能性 比如: (1),(2); (1,1)、(1,2)、(2,1)、(2,1),在PHP上使用自定义函数: $inputValues = range(1,3); //here you set your i
input : 2
output :
(1) , (2);
(1,2), (2,2);
input : 3
output :
(1),(2),(3);
(1,2),(1,3),(2,3);
(1,2,3)
公式为2^n-1
我只试过用一次。我想得到所有的可能性
比如:(1),(2);
(1,1)、(1,2)、(2,1)、(2,1),
在
PHP
上使用自定义函数:
$inputValues = range(1,3); //here you set your input(2 or 3 or what you want)
$result = array();
function variations($inputValues, $level, &$result, $current = array()) {
for($i = 0; $i < count($inputValues); $i++) {
$new = array_merge($current, array($inputValues[$i]));
if($level == 1) {
sort($new);
if (!in_array($new, $result)) {
$result[] = $new;
}
} else {
variations($inputValues, $level - 1, $result, $new);
}
}
}
for ($i = 0; $i<count($inputValues); $i++) {
variations($inputValues, $i+1, $result);
}
foreach ($result as $arr) {
echo '('.join(",", $arr) . ')';
}
研究列举所有可能的排列。网上有大量的例子,让我们看看你的尝试。我们不会为你做所有的工作。公式不是2^n-1。!对不起。我在这方面还是很差。谢谢你。这对我来说已经足够了。谢谢:D@sergio
(1)(2)(3)(1,1)(1,2)(1,3)(2,2)(2,3)(3,3)(1,1,1)(1,1,2)(1,1,3)(1,2,2)(1,2,3)(1,3,3)(2,2,2)(2,2,3)(2,3,3)(3,3,3)