Last active
March 7, 2017 15:49
-
-
Save dmekhov/458f3ddfc3c5bb1090ced713fed78cda to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Представим шахматную доску в виде 2-х мерной матрицы. В белых и чёрных клетках вписаны цифры. | |
* Левая верхняя клетка у шахматной доски белая. | |
* Задача: транспонировать матрицу вокруг главной диагонали, но не трогать цифры в белых клетках. | |
*/ | |
function transpose($array) { | |
$A = $B = $array; | |
for($i = 0; $i < count($A); $i++) { | |
for($j = 0; $j < count($A); $j++) { | |
if(!($i%2)) $j++; | |
$B[$j][$i] = $A[$i][$j]; | |
if($i%2) $j++; | |
} | |
} | |
return $B; | |
} | |
for ($i = 0; $i < 8; $i++){ | |
for ($j = 0; $j < 8; $j++){ | |
$array[$i][$j] = rand(0, 100); | |
} | |
} | |
print_r($array); | |
print_r(transpose($array)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment