简体   繁体   English

更有效的方法来完成这个?

[英]More Efficient Way to Accomplish This?

I need to know the level of a player using the amount of exp he has and the exp chart. 我需要知道玩家使用exp数量和exp图表的等级。 I want to do it the most efficient way possible. 我想以最有效的方式做到这一点。 This is what I got. 这就是我得到的。 Note: The real expChart has thousands of levels/index. 注意:真正的expChart有数千个级别/索引。 All the values are in increasing order. 所有值都在递增。

var expChart = [1,10,23,54,65,78,233,544,7666,22224,64654,456456,1123442];
/*
lvl 0: //0-1[ exp
lvl 1: //[1-10[ exp
lvl 2: //[10-23[ exp
*/
getLvlViaExp = function(exp){
    for(var i = 0 ; i < expChart.length ; i++){
        if(exp < expChart[i]) break;
    }
    return i;
}

This is a more efficient way to do it. 这是一种更有效的方法。 Every x steps, (6 i the example, probably every hundreds with real chart), I do a quick comparation and jump to approximative index, skipping many indexes. 每个x步骤(例如,6个实例,可能每百个有真实图表),我做一个快速比较并跳转到近似索引,跳过许多索引。

getLvlViaExp = function(exp){
    var start = 0;
    if(exp > 233)   start = 6;
    if(exp > 1123442) start = 12;

    for(var i = start ; i < expChart.length ; i++){
        if(exp < expChart[i]) break;
    }
    return i;
}

Is there an even better way to do this? 还有更好的方法吗?


SOLUTION: 解:

Array.prototype.binarySearch = function(value){
    var startIndex  = 0,
        stopIndex   = this.length - 1,
        middle      = Math.floor((stopIndex + startIndex)/2);
    if(value < this[0]) return 0;
    while(!(value >= this[middle] && value < this[middle+1]) && startIndex < stopIndex){

        if (value < this[middle]){
            stopIndex = middle - 1;
        } else if (value > this[middle]){
            startIndex = middle + 1;
        }

        middle = Math.floor((stopIndex + startIndex)/2);
    }

    return middle+1;
}

The best algorithm for searching is binary search which is O(lg n) (unless you can do it with a hashing search which is O(c). 最好的搜索算法是二进制搜索,即O(lg n)(除非您可以使用哈希搜索O(c)进行搜索)。

http://www.nczonline.net/blog/2009/09/01/computer-science-in-javascript-binary-search/ http://www.nczonline.net/blog/2009/09/01/computer-science-in-javascript-binary-search/

Basically jump to the middle of your chart ( n / 2). 基本上跳到图表的中间(n / 2)。 Is you experience higher or lower from that number. 您是否从该数字中获得更高或更低的体验。 If higher jump to the middle higher half. 如果更高跳到中间的上半部分。 If lower jump to the middle of the lower half: Compare and repeat until you find what you're looking for. 如果下跳到下半部分的中间:比较并重复,直到找到你要找的东西。

var expChart = [1,10,23,54,65,78,233,544,7666,22224,64654,456456,1123442];
getLvlViaExp = function(exp){
    var min=0;
    var max=expChart.length-1;
    var i;
    while(min <=max){

    i=Math.round((min+max)/2); 
        //document.write("<br />"+i+":"+min+":"+max+"<br />");
        if(exp>=expChart[i] && exp <=expChart[i+1]) {
        break;
        }
    if(exp>=expChart[i]){
            min=i+1;
        }
    if(exp<=expChart[i]){
            max=i-1;

        }

    }
return i;
}

document.write(getLvlViaExp("10"));

I have tested it and it seems to work pretty well. 我测试了它,似乎工作得很好。 If you want to see how many steps it actually goes through to get to the answer, uncomment the document.write in the while loop. 如果您想查看实际获得答案的步骤数,请在while循环中取消注释document.write。 It was kind of fascinating watching it. 看着它真是令人着迷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM