Home > Backend Development > PHP Tutorial > PHP permutation and combination algorithm

PHP permutation and combination algorithm

WBOY
Release: 2016-07-29 09:10:50
Original
1562 people have browsed it

I have always been interested in algorithms. When I was training on iOS at the beginning of last year, the teacher gave a ranking question in class to see who could express his ideas first, but he did not require immediate coding to implement it. At that time, a first-year student from the University of Beijing Science and Technology quickly said I found a feasible method and was affirmed by the teacher. It sounded simple but it was not as easy to understand as I imagined. Later, I didn’t think about various things in detail.

In fact, I tried to write the lottery numbers in C as early as my college days. I played Welfare Lottery 3D and Sports Lottery Number 3 for a while, trying to use computers to screen possible winning numbers. At that time, I had little knowledge and was completely driven by interest. Fortunately, I was With no financial resources and limited living expenses, I was able to get out of this road of no return in time.

  The recent mobile game project mentioned that it requires permutation and combination algorithms. Although I am working on the client now, I am still relatively familiar with PHP, so I practiced by myself and tried to write it. Unexpectedly, it turned out to be the array function of PHP. It is so convenient to use, and it can be done in just a few lines. Now I share it with everyone. I hope you can give me some advice on the shortcomings.

<?php

// 阶乘
function factorial($n) {
	return array_product(range(1, $n));
}

// 排列数
function A($n, $m) {
	return factorial($n)/factorial($n-$m);
}

// 组合数
function C($n, $m) {
	return A($n, $m)/factorial($m);
}

// 排列
function arrangement($a, $m) {
	$r = array();

	$n = count($a);
	if ($m <= 0 || $m > $n) {
		return $r;
	}

	for ($i=0; $i<$n; $i++) {
		$b = $a;
		$t = array_splice($b, $i, 1);
		if ($m == 1) {
			$r[] = $t;
		} else {
			$c = arrangement($b, $m-1);
			foreach ($c as $v) {
				$r[] = array_merge($t, $v);
			}
		}
	}

	return $r;
}

// 组合
function combination($a, $m) {
	$r = array();

	$n = count($a);
	if ($m <= 0 || $m > $n) {
		return $r;
	}

	for ($i=0; $i<$n; $i++) {
		$t = array($a[$i]);
		if ($m == 1) {
			$r[] = $t;
		} else {
			$b = array_slice($a, $i+1);
			$c = combination($b, $m-1);
			foreach ($c as $v) {
				$r[] = array_merge($t, $v);
			}
		}
	}

	return $r;
}


// ====== 测试 ======
$a = array("A", "B", "C", "D");

$r = arrangement($a, 2);
var_dump($r);

$r = A(4, 2);
echo $r."\n";

$r = combination($a, 2);
var_dump($r);

$r = C(4, 2);
echo $r."\n";

Copy after login

In addition, when I was training PHP a few years ago, I also wrote a loop matrix algorithm. I am not allowed to use external links here. Interested students can Baidu "A written test question about algorithms from Sina at the beginning of the month". The second article is my original version for everyone to learn and exchange.

The above introduces the PHP permutation and combination algorithm, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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