• 四人过桥js简单实现


    一座桥,同时只能两个人过桥,他们共用一个手电筒。现有四个人,

    每个人的过桥时间都不一样,分别为:10、5、2、1。怎样让他们用最短的时间过去。

     

     

    <html lang="zh-cn">
    <head>
    <meta charset="utf-8"/>
    
    <script type="text/javascript">
    <!--
        // 一座桥,同时只能两个人过桥,他们共用一个手电筒。现有四个人,
        //每个人的过桥时间都不一样,分别为:10、5、2、1。怎样让他们用最短的时间过去。
    
        var passer = function(name,time){
            this.name = name;
            this.time = time;
        }
        
        passer.prototype = {
            //重写toString方法  
            toString : function() {  
                return this.name;  
            }
        }
    
        var passBridge=function(tempArr){
            this.backarray = [];         //未过桥数组
            this.passarray = [];         //过桥数组
            this.backarray = tempArr;
            
            this.amounttime = 0;  //总时长
            this.passcount = 1;   //过桥次数
            document.write("" + this.backarray.length + "人:" + this.backarray + ".<br>" + this.description() + "<br><br><br>");
        }
        
        passBridge.prototype.description = function (){
            var res="";    
            for(var o in this.backarray){    
                res+=(this.backarray[o].name+"的时间是"+ this.backarray[o].time +"<br>");//从当前this对象读取构造器中的属性对应的值    
            }    
            return res;
        }
    
        passBridge.prototype.pass = function(){
            var first,second;
            var lastone,lasttwo;
            this.backarray.sort(function(a,b){return a.time - b.time;});
            if(this.passcount % 2 != 0){   //单次数过桥,由未过桥数组中最小的两个人过桥
                first = this.backarray[0];
                second = this.backarray[1];
                this.amounttime += second.time;
                this.backarray.splice(0,2);
                document.write(first.name + second.name +"过桥:" + this.backarray + "  ____________"+ first.name +""+ second.name +"웃⇢_________  " + this.passarray + "<br>" + "用时:" +second.time + "<br>");
                this.passarray.push(first);
                this.passarray.push(second);
            }else{                            //双次数过桥,由未过桥数组中最大的两个人过桥
                lastone = this.backarray[this.backarray.length-1];
                lasttwo = this.backarray[this.backarray.length-2];
                this.amounttime += lastone.time;
                this.backarray.splice(this.backarray.length-2,2);
                document.write(lastone.name + lasttwo.name +"过桥:" + this.backarray + "  ____________"+ lastone.name  +""+ lasttwo.name +"웃⇢_________  " + this.passarray + "<br>" + "用时:" +lastone.time + "<br>");
                this.passarray.push(lastone);
                this.passarray.push(lasttwo);
            }
            this.passcount++;
            if(this.backarray.length == 0){            
                document.write("<br><br><br>成功过桥,总时长为:" + this.amounttime);
            }else{
                this.back();
            }
        }
    
        passBridge.prototype.back = function(){
            this.passarray.sort(function(a,b){return a.time - b.time;});
            var temp = this.passarray[0]
            this.amounttime += this.passarray[0].time;
            //document.write(this.passarray[0].name + "正在返回,<br><br><br>时间为:" + this.passarray[0].time + "。没过桥的有" + this.backarray);
            this.passarray.splice(0,1);
            document.write(temp.name +"返回:" + this.backarray + "  ___________⇠웃"+ temp.name +"__________  " + this.passarray + "<br>" + "用时:" +temp.time + "<br><br><br>");
            this.backarray.push(temp);
            this.pass();
        }
     
        var tempArr = [new passer('a',1),new passer('b',2),new passer('c',5),new passer('d',10),new passer('e',11),new passer('f',12)];
        var main = new passBridge(tempArr);
        window.onload = main.pass();
    //-->
    </script>
    </head>
    </html>

     

     由什么需要改进的,希望看官能提出来。

  • 相关阅读:
    9-2 链表
    transient
    获取当前电脑的ip地址
    LinkList
    Java代码模拟链表
    什么是复合主键
    wp8 入门到精通
    C# 从入门到精通
    wp8 json2csharp
    wp8 安装.Net3.5
  • 原文地址:https://www.cnblogs.com/PPBoy/p/1961312.html
Copyright © 2020-2023  润新知