Skip to content

Instantly share code, notes, and snippets.

@quangpd
Created May 12, 2016 03:40
Show Gist options
  • Save quangpd/da775fdefd82717d2868cd2454a334cf to your computer and use it in GitHub Desktop.
Save quangpd/da775fdefd82717d2868cd2454a334cf to your computer and use it in GitHub Desktop.
<?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