Created
May 12, 2016 03:40
-
-
Save quangpd/da775fdefd82717d2868cd2454a334cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
*/ | |
class ToHop | |
{ | |
public $a = array(); | |
public $n; | |
public $k; | |
function __construct($n, $k) | |
{ | |
$this->a[0] = 0; | |
$this->n = $n; | |
$this->k = $k; | |
} | |
function run() | |
{ | |
$this->backtrack(1); | |
} | |
function backtrack($i) | |
{ | |
for ($j = $this->a[$i - 1] + 1; $j <= $this->n - $this->k + $i; $j++) | |
{ | |
$this->a[$i] = $j; | |
($i == $this->k) ? $this->display() : $this->backtrack($i + 1); | |
} | |
} | |
function display() | |
{ | |
for ($j = 1; $j <= $this->k; $j++) | |
{ | |
printf("%3d", $this->a[$j]); | |
} | |
echo "\n"; | |
} | |
} | |
$th = new ToHop(8, 5); | |
$th->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment