Home > Backend Development > PHP Tutorial > PHP数三退1面向对象的写法

PHP数三退1面向对象的写法

WBOY
Release: 2016-06-13 13:11:38
Original
926 people have browsed it

PHP数3退1面向对象的写法

<?php //用面向对象的方法写 将猴子选大王实现 同样数3退1
        class Monkey {
                /**
                 * 定义猴子类 
                 * 属性  编号 $id    
                 *       左手 $left  表示的当前猴子左边的猴子
                 *       右手 $right 表示的当前猴子右边的猴子
                */
                        public $id;
                        public $left;
                        public $right;
        }
        class Circle{
                /**
                 * 定义圈类 
                 * 属性  猴子的总数 $ct    
                 *       开始的猴子 $first 
                 *       结束的猴子 $last  
                 */
                public $ct = 0;
                public $first;
                public $last;
                //构造方法创建圈
                //参数是猴子的个数
                function __construct($c){
                        for($i=0;$i<$c;$i++){
                                $this->add();
                        }
                }

                //猴子的添加方法
                function add(){
                        $m = new Monkey();
                        $m->id = $this->ct;
                        if($this->ct==0){
                                $this->first = $m;
                                $this->last = $m;
                                $m->left = $m;
                                $m->right = $m;
                        }else{
                                $this->last->right = $m;
                                $m->left = $this->last;
                                $m->right = $this->first;
                                $this->first->left = $m;
                                $this->last = $m;
                        }
                        $this->ct++;
                }

                //猴子的删除方法
                function del($m){
                        if($this->ct ct == 1){
                        
                                $this->first = $this->last = null;
                        }else{
                                $m->left->right = $m->right;
                                $m->right->left = $m->left;

                                if($m == $this->first){
                                        $this->first = $m->right;
                                }else if($m == $this->last){
                                        $this->last = $m->left;
                                }
                        }
                        $this->ct--;
                }
        
        }
        /*
                这里的代码就是实现让500只猴子围城一圈然后
                数三个退一个就这样直到还剩一个把那猴子的编号打印出来
        */
        $circle = new Circle(500);
        $number = 0;
        $mk = $circle->first;
        while($circle->ct > 1){
                $number++;
                if($number == 3){
                        $number = 0;
                        $circle->del($mk);
                }
                $mk = $mk->right;
        }
        echo $circle->first->id;
?> 


Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template