


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 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:
<?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); ?>
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 )
I hope this article will be helpful to everyone’s PHP programming design.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software