一个算法的小问题
想求一个算法,比如我知道一个数 是 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;}
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
$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;}
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
这个会有重复的
楼主说过: 可以重复 位置顺序也不固定
$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;}
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
这个会有重复的
$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;}
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
这个会有重复的
楼主说过: 可以重复 位置顺序也不固定
$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;}
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
这个会有重复的
$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;}
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
这个会有重复的
-------------------------------
如果不重复该如何写?想了下,没想出来
$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;}
8 + 2 + 32 + 7 + 49 + 1 + 32 + 6 + 50 + 4 + 92 + 7 + 44 + 8 + 15 + 8 + 08 + 4 + 19 + 0 + 4
=====================
<?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; } } } ?>
这样写效率是不是很差?
其实你都没描述清楚你的需求:
你是只需要随机出一个结果就好呢?还是要列出所有可能?或者其它指定个结果?
只需要一个随机的组合就行了
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;}
自己写了一个 求点评,不知道有没有缺陷 或效率怎么样
穷举然后再随机抽取
只需要一个随机的组合就行了
那简单啊。如下:
$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);
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);}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
