Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Zig-zag-matrix/PHP/zig-zag-matrix.php
Normal file
32
Task/Zig-zag-matrix/PHP/zig-zag-matrix.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
function ZigZagMatrix($num) {
|
||||
$matrix = array();
|
||||
for ($i = 0; $i < $num; $i++){
|
||||
$matrix[$i] = array();
|
||||
}
|
||||
|
||||
$i=1;
|
||||
$j=1;
|
||||
for ($e = 0; $e < $num*$num; $e++) {
|
||||
$matrix[$i-1][$j-1] = $e;
|
||||
if (($i + $j) % 2 == 0) {
|
||||
if ($j < $num){
|
||||
$j++;
|
||||
}else{
|
||||
$i += 2;
|
||||
}
|
||||
if ($i > 1){
|
||||
$i --;
|
||||
}
|
||||
} else {
|
||||
if ($i < $num){
|
||||
$i++;
|
||||
}else{
|
||||
$j += 2;
|
||||
}
|
||||
if ($j > 1){
|
||||
$j --;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $matrix;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue