zoukankan      html  css  js  c++  java
  • php php 顺时针或逆时针输出连续数字

    <?php
    
    class Circle
    {
        private $start;
        private $is_clockwise;
        private $start_direction;
        private $is_asc;
        private $width;
        private $arr;
    
        public function __construct($params)
        {
            $this->start = $params['start'];
            $this->is_clockwise = isset($params['is_clockwise']) ? $params['is_clockwise'] : 0;
            $this->start_direction = isset($params['start_direction']) ? $params['start_direction'] : 1;
            $this->is_asc = isset($params['is_asc']) ? $params['is_asc'] : 1;
            $this->width = $params['width'];
        }
    
        public function getNums()
        {
            $i = 1;
            $p1 = $this->start;
            $circle_top = ceil($this->width / 2);
    
            if ($this->is_asc == 1) {
                $num_end = $this->width * $this->width + $this->start;
                while ($i <= $circle_top && $p1 <= $num_end) {
                    if ($this->is_clockwise == 1) {
                        $this->arr[$i - 1][$i - 1] = $p1;
                        $p2 = $this->hor_add($p1 + 1, $i - 1, $i, $this->width - 2 * $i + 1);
                        $p3 = $this->ver_add($p2, $i, $this->width - $i, $this->width - 2 * $i + 1);
                        $p4 = $this->hor_reduce($p3, $this->width - $i, $this->width - $i - 1, $this->width - 2 * $i + 1);
                        $p1 = $this->ver_reduce($p4, $this->width - $i - 1, $i - 1, $this->width - 2 * $i);
                        $i++;
                    } else {
                        $this->arr[$i - 1][$i - 1] = $p1;
                        $p2 = $this->ver_add($p1 + 1, $i, $i - 1, $this->width - 2 * $i + 1);
                        $p3 = $this->hor_add($p2, $this->width - $i, $i, $this->width - 2 * $i + 1);
                        $p4 = $this->ver_reduce($p3, $this->width - $i - 1, $this->width - $i, $this->width - 2 * $i + 1);
                        $p1 = $this->hor_reduce($p4, $i - 1, $this->width - $i - 1, $this->width - 2 * $i);
                        $i++;
                    }
                }
            } else {
                $num_end = $this->start - $this->width * $this->width;
                while ($i <= $circle_top && $p1 >= $num_end) {
                    if ($this->is_clockwise == 1) {
                        $this->arr[$i - 1][$i - 1] = $p1;
                        $p2 = $this->hor_add($p1 - 1, $i - 1, $i, $this->width - 2 * $i + 1);
                        $p3 = $this->ver_add($p2, $i, $this->width - $i, $this->width - 2 * $i + 1);
                        $p4 = $this->hor_reduce($p3, $this->width - $i, $this->width - $i - 1, $this->width - 2 * $i + 1);
                        $p1 = $this->ver_reduce($p4, $this->width - $i - 1, $i - 1, $this->width - 2 * $i);
                        $i++;
                    } else {
                        $this->arr[$i - 1][$i - 1] = $p1;
                        $p2 = $this->ver_add($p1 - 1, $i, $i - 1, $this->width - 2 * $i + 1);//var_dump($this->arr,$p2);die;
                        $p3 = $this->hor_add($p2, $this->width - $i, $i, $this->width - 2 * $i + 1);
                        $p4 = $this->ver_reduce($p3, $this->width - $i - 1, $this->width - $i, $this->width - 2 * $i + 1);
                        $p1 = $this->hor_reduce($p4, $i - 1, $this->width - $i - 1, $this->width - 2 * $i);
                        $i++;
                    }
                }
            }
    
            foreach ($this->arr as $key => $item) {
                ksort($item);
                $this->arr[$key] = $item;
            }
    
            $this->rotate();
            $this->dump($this->arr);
        }
    
        private function hor_add($start, $x, $y, $num)
        {
            if ($this->is_asc == 1) {
                for ($i = $start; $i < $start + $num; $i++) {
                    $this->arr[$x][$y] = $i;
                    ++$y;
                }
                return $start + $num;
            } else {
                for ($i = $start; $i > $start - $num; $i--) {
                    $this->arr[$x][$y] = $i;
                    ++$y;
                }
                return $start - $num;
            }
        }
    
        private function ver_add($start, $x, $y, $num)
        {
            if ($this->is_asc == 1) {
                for ($i = $start; $i < $start + $num; $i++) {
                    $this->arr[$x][$y] = $i;
                    ++$x;
                }
                return $start + $num;
            } else {
                for ($i = $start; $i > $start - $num; $i--) {
                    $this->arr[$x][$y] = $i;
                    ++$x;
                }
                return $start - $num;
            }
        }
    
        private function hor_reduce($start, $x, $y, $num)
        {
            if ($this->is_asc == 1) {
                for ($i = $start; $i < $start + $num; $i++) {
                    $this->arr[$x][$y] = $i;
                    $y--;
                }
                return $start + $num;
            } else {
                for ($i = $start; $i > $start - $num; $i--) {
                    $this->arr[$x][$y] = $i;
                    $y--;
                }
                return $start - $num;
            }
        }
    
        private function ver_reduce($start, $x, $y, $num)
        {
            if ($this->is_asc == 1) {
                for ($i = $start; $i < $start + $num; $i++) {
                    $this->arr[$x][$y] = $i;
                    $x--;
                }
                return $start + $num;
            } else {
                for ($i = $start; $i > $start - $num; $i--) {
                    $this->arr[$x][$y] = $i;
                    $x--;
                }
                return $start - $num;
            }
        }
    
        private function dump($arr)
        {
            $len = count($arr);
            $html = '<table border="1px solid silver" >';
            for ($i = 0; $i < $len; ++$i) {
                $html .= '<tr >';
                for ($j = 0; $j < $len; ++$j) {
                    $html .= '<td width="50px" height="50px">' . $arr[$i][$j] . '</td>';
                }
                $html .= '</tr>';
            }
            $html .= '</table>';
            echo $html;
        }
    
        private function rotate()
        {
            $arr = [];
            if ($this->start_direction == 1) {
                return;
            } else if ($this->start_direction == 3) {
                $arr = $this->arr;
                for ($i = 0; $i < $this->width; ++$i) {
                    for ($j = 0; $j < $this->width; ++$j) {
                        $this->arr[$i][$j] = $arr[$i][$this->width - $j - 1];
                    }
                }
            } else if ($this->start_direction == 2 || $this->start_direction == 4) {
                for ($i = 0; $i < $this->width; ++$i) {
                    for ($j = 0; $j < $this->width; ++$j) {
                        $arr[$i][$j] = $this->arr[$j][$i];
                    }
                }
                for ($i = 0; $i < $this->width; ++$i) {
                    for ($j = 0; $j < $this->width; ++$j) {
                        if ($this->start_direction == 2) {
                            $this->arr[$i][$j] = $arr[$i][$this->width - $j - 1];
                        } else {
                            $this->arr[$i][$j] = $arr[$this->width - $i - 1][$j];
                        }
                    }
                }
            }
        }
    }
    
    $num = new Circle(['is_asc' => 0, 'start' => 9, 'width' => 3, 'is_clockwise' => 1, 'start_direction' => 1]);
    $num->getNums();

     

  • 相关阅读:
    ffmpeg解码视频存为BMP文件
    iphoneOS与Windwos下RTSP服务器与客户端的搭建
    RTSP协议介绍
    还原数据库
    .NET导入Excel数据
    jquery对表单元素操作
    Jquery操作select
    The process cannot access the file '' because it is being used by another process.....
    aspnet_merge.exe”已退出,代码为1的错误
    vs2008运行正常(可以访问数据库),而iis不能正常访问数据库
  • 原文地址:https://www.cnblogs.com/tysdeblog/p/6588520.html
Copyright © 2011-2022 走看看