Home > Backend Development > PHP Tutorial > 一个算法的小问题

一个算法的小问题

WBOY
Release: 2016-06-20 12:39:06
Original
943 people have browsed it

想求一个算法,比如我知道一个数 是 13   然后我需要随机从 0-9 的10个数字中取 3个数  3个数的和就是 13,比如可以是 5+5+3  也可以是  3+5+5  或 5+3+5     6+2+5    可以重复  位置顺序也不固定。

php 怎么实现呢。


回复讨论(解决方案)

既然有最终值的要求,就不能三个数都随机了吧,最多只能随机一个数,其他的从余值范围的数值中在次随机取

$num = 13;$a = array_merge(range(0, 9), range(0, 9), range(0, 9));$loop = 10;while($loop--) {  shuffle($a);  foreach(array_chunk($a, 3) as $v)    if(array_sum($v) == $num) echo join(' + ', $v), PHP_EOL;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

$num = 13;$a = array_merge(range(0, 9), range(0, 9), range(0, 9));$loop = 10;while($loop--) {  shuffle($a);  foreach(array_chunk($a, 3) as $v)    if(array_sum($v) == $num) echo join(' + ', $v), PHP_EOL;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


这个会有重复的

楼主说过: 可以重复 位置顺序也不固定


$num = 13;$a = array_merge(range(0, 9), range(0, 9), range(0, 9));$loop = 10;while($loop--) {  shuffle($a);  foreach(array_chunk($a, 3) as $v)    if(array_sum($v) == $num) echo join(' + ', $v), PHP_EOL;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


这个会有重复的




$num = 13;$a = array_merge(range(0, 9), range(0, 9), range(0, 9));$loop = 10;while($loop--) {  shuffle($a);  foreach(array_chunk($a, 3) as $v)    if(array_sum($v) == $num) echo join(' + ', $v), PHP_EOL;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


这个会有重复的

楼主说过: 可以重复 位置顺序也不固定



$num = 13;$a = array_merge(range(0, 9), range(0, 9), range(0, 9));$loop = 10;while($loop--) {  shuffle($a);  foreach(array_chunk($a, 3) as $v)    if(array_sum($v) == $num) echo join(' + ', $v), PHP_EOL;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


这个会有重复的




$num = 13;$a = array_merge(range(0, 9), range(0, 9), range(0, 9));$loop = 10;while($loop--) {  shuffle($a);  foreach(array_chunk($a, 3) as $v)    if(array_sum($v) == $num) echo join(' + ', $v), PHP_EOL;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


这个会有重复的



-------------------------------
如果不重复该如何写?想了下,没想出来

$num = 13;$a = array_merge(range(0, 9), range(0, 9), range(0, 9));$loop = 10;while($loop--) {  shuffle($a);  foreach(array_chunk($a, 3) as $v)    if(array_sum($v) == $num) echo join(' + ', $v), PHP_EOL;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



=====================
<?php	$sum = 13;	for($a=0; $a<10; $a++){		for($b=0; $b<10; $b++){			$c = $sum-$a-$b;			if($c<10 && $c>0){				echo $a.' + '.$b.' + '.$c,PHP_EOL;			}		}	}		?>
Copy after login

这样写效率是不是很差?

其实你都没描述清楚你的需求:
你是只需要随机出一个结果就好呢?还是要列出所有可能?或者其它指定个结果?

只需要一个随机的组合就行了

3个数可以重复出现

function number($num){    if( $num == 0 ) return array(0, 0, 0);    if( $num == 27 ) return array(9, 9, 9);    $data = array();        $oneMax = ( $num > 10 ) ? 9 : $num;    $oneMin = ($num > 18) ? ($num - 18) : 0;    $data[0] = rand($oneMin, $oneMax);        $twoMax = ( ($num - $data[0]) > 9 ) ? 9 : ($num - $data[0]);    $twoMin = ( ($num - $data[0]) > 9 ) ? ($num - $data[0] - 9) : 0;    $data[1] = rand($twoMin, $twoMax);        $data[2] = $num - $data[0] - $data[1];    return $data;}
Copy after login


自己写了一个 求点评,不知道有没有缺陷 或效率怎么样

穷举然后再随机抽取

只需要一个随机的组合就行了


那简单啊。如下:
        $sum = 13;//要求的和,不能超过27        $result = array(rand(0,9));//第一个随机数,下面是第二个随机数,要控制范围,否则第三个数可能就不在范围内        if($result[0]<$sum-9)            $result[] = rand($sum-9-$result[0],9);        else            $result[] = rand(0,$sum-$result[0]);        $result[] = $sum-array_sum($result);//和减去已经生成的两个随机数,就是第三个数。        var_dump($result);
Copy after login

echo number(13);function number($num) {  $r[] = rand(1, 9);  $r[] = rand(1, min(9, $num - $r[0]));  $r[] = $num - $r[0] - $r[1];  return join('+', $r);}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template