Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java或php中编写查找概率的代码_Java_Php_Probability - Fatal编程技术网

如何在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)