JavaScriptは循環行列とジョセフ環問題の解決を実現します.


    function MyCircularQueue(){
        var items = [];
        //       
        this.enQueue = function(value){
            return items.push(value);
        }
        //    
        this.deQueue = function(){
            return items.shift();
        }
        //      
        this.front = function(){
            return items[0];
        }
        //    
        this.print = function(){
            return items.toString();
        }
        //      
        this.size = function(){
            return items.length;
        }
        //        
        this.isEmpty = function(){
            return items.length == 0;
        }
    }

    /*      
     *         ,     n   ,
     *               ,            
     *     。
     */
     /*  
     *nameList         
     *n        
     */
     function getLastPerson(nameList, n){
        var queue = new MyCircularQueue();
        //       
        for(var i = 0; i < nameList.length; i++){
            queue.enQueue(nameList[i]);
        }
        var loser = '';
        while(queue.size() > 1){
            for(var i = 0; i < n; i++){
                queue.enQueue(queue.deQueue());
            }
            loser = queue.deQueue();
            console.log('    ' + queue.print() + ' ' + '      :' + loser);
        }
        return queue.deQueue(); 
     }
     var nameList = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
     var winner = getLastPerson(nameList, 6);
     console.log('   :' + winner);