Matrix arrangement issues in php

前端 未结 6 1026
闹比i
闹比i 2021-02-20 13:07

I would like to know some solutions to such a problem.

It is given a number lets say 16 and you have to arrange a matrix this way

1  2  3  4
12 13 14 5
         


        
6条回答
  •  别那么骄傲
    2021-02-20 14:03

    [EDIT: Update] If language doesn't matter:

    Go to: http://rosettacode.org/wiki/Spiral_matrix


    In PHP:

    Here you go:

     0);
    
        return $result;
    }
    
    function PrintArray($array)
    {
        for ($i = 0; $i < count($array); $i++) {
            for ($j = 0; $j < count($array); $j++) {
                echo str_pad($array[$i][$j],3,' ');
            }
            echo '
    '; } } $arr = getSpiralArray(4); echo '
    ';
    PrintArray($arr);
    echo '
    '; ?>

提交回复
热议问题