Table of Contents
PHP uses permutation and combination to realize that the sum of numbers 1 to 9 is equal to 20. PHP permutation and combination
Home Backend Development PHP Tutorial PHP uses permutation and combination to realize that the sum of numbers from 1 to 9 is equal to 20. PHP permutation and combination_PHP tutorial

PHP uses permutation and combination to realize that the sum of numbers from 1 to 9 is equal to 20. PHP permutation and combination_PHP tutorial

Jul 13, 2016 am 09:45 AM
php arrangement number combination

PHP uses permutation and combination to realize that the sum of numbers 1 to 9 is equal to 20. PHP permutation and combination

This article describes the example of php using permutation and combination to realize the addition of numbers 1 to 9. All equal to 20. Share it with everyone for your reference. The specific implementation method is as follows:

<&#63;php
set_time_limit(0);
/*
函数说明:huoqu_zhuhe($eq,$jiashu,$isone=0)
参数说明:$eq---几个数相加的总和;
 $jiashu-------加数数组:$jiashu=array(1,2,3,4,5,6,7,8,9),可以使用的加数;
 $isone---是否要每次使用不同的加数,唯一性,1是 0 不,默认1
返回类型:数组,数字以+相连的字符串:[0] => 3+8+9 [1] => 4+7+9
测试效果:1:对于加数数组比较小的,速度可以,过大的话,有些慢;2:每次可以使用不同的加数的,处理会变慢
采用的方法是:生成所有可能排列,对排列处理过滤重复的,得到组合
*/
function huoqu_zhuhe($eq,$jiashu,$isone=1)
{if(empty($jiashu)||!is_array($jiashu)){echo 'error:加数必须数组';return false;}
$feishu=0;
for($i=0;$i<count($jiashu);$i++){
if(!is_numeric($jiashu[$i])){$feishu=1;break;}
}
if($feishu==1){echo 'error;数组中必须是合法的数字';return false;}
$lian=$jiashu;
$savearr=array();
while(!empty($lian)){
//echo 1;
$newarr=array();
$k=0;
for($i=0;$i<count($lian);$i++){
$lianstr=$lian[$i];
$arr=explode('+',$lianstr);
$nowhe=array_sum($arr);
//echo $nowhe;
for($j=0;$j<count($jiashu);$j++){
$savestr=$lianstr.'+'.$jiashu[$j];
if($isone==1&&in_array($jiashu[$j],$arr))continue;
if(($nowhe+$jiashu[$j])>$eq)break;
else if(($nowhe+$jiashu[$j])==$eq){
$savearr[]=$savestr;
}
else{$newarr[$k]=$savestr;$k++;}
}//end for($j=0;$j<count($jiashu)
}// end for($i=0;$i
$lian=$newarr;
}//end while(!empty($lian))
//print_r($savearr);
//生成组合部分,过滤重复,2个数组以一个为参考,看另一个是否能通过移动达到匹配,可以,过滤
$isguolu=array();//存储对应的id的取舍 0取 1舍
for($i=0;$i<count($savearr);$i++){
$isguolu[]=0;
}//初始化全部0
for($i=0;$i<count($savearr);$i++){
$arr1=explode('+',$savearr[$i]);
$len1=count($arr1);
for($j=$i+1;$j<count($savearr);$j++){
$arr2=explode('+',$savearr[$j]);
$len2=count($arr2);
if($len1!=$len2)continue;
if($isguolu[$j]==1)continue;
//比较$arr1和$arr2开始
$jishu=0;
for($i1=0;$i1<count($arr1);$i1++){
$a=$arr1[$i1];
$isyou=0;
for($i2=$i1;$i2<count($arr2);$i2++){
if($a==$arr2[$i2]){
$jishu++;
$isyou=1;
$t=$arr2[$i1];
$arr2[$i1]=$arr2[$i2];
$arr2[$i2]=$t;
break;
}
}//end for($i2=0
if($isyou==0)break;
}// end for($i1=0;$i1<count($arr1);
if($jishu==$len1)$isguolu[$j]=1;
}//end for($j=$i+1;
}//end for($i=0;$i<count($savearr);$i++)
//print_r($isguolu);
//根据过滤数组选择
$newarr=array();
for($i=0;$i<count($savearr);$i++){
if($isguolu[$i]==0)$newarr[]=$savearr[$i];
}
//print_r($newarr);
return $newarr;
}
//下面是一个测试
//取用1,2,3,4,5,6,7,8,9相加所有等于20的组合
$jiashu=array(1,2,3,4,5,6,7,8,9);
$eq=20;
if($jieguo=huoqu_zhuhe($eq,$jiashu,1))print_r($jieguo);
&#63;>

Copy after login

The running results are as follows:

Array
(
  [0] => 3+8+9
  [1] => 4+7+9
  [2] => 5+6+9
  [3] => 5+7+8
  [4] => 1+2+8+9
  [5] => 1+3+7+9
  [6] => 1+4+6+9
  [7] => 1+4+7+8
  [8] => 1+5+6+8
  [9] => 2+3+6+9
  [10] => 2+3+7+8
  [11] => 2+4+5+9
  [12] => 2+4+6+8
  [13] => 2+5+6+7
  [14] => 3+4+5+8
  [15] => 3+4+6+7
  [16] => 1+2+3+5+9
  [17] => 1+2+3+6+8
  [18] => 1+2+4+5+8
  [19] => 1+2+4+6+7
  [20] => 1+3+4+5+7
  [21] => 2+3+4+5+6
)
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1042687.htmlTechArticlephp uses permutation and combination to realize the method of adding up the numbers 1 to 9 to equal 20. The example of php permutation and combination is described in this article PHP uses permutation and combination to realize the method of adding up the numbers 1 to 9 equal to 20. Share with everyone...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

See all articles