用js来实现银行家算法

 

Number.prototype.round = function (len) {
var old = this;
var a1 = Math.pow(10, len) * old;
a1 = Math.round(a1);
var oldstr = old.toString()
var start = oldstr.indexOf(“.”);
if (start > 0 && oldstr.split(“.”)[1].length == len + 1) {
if (oldstr.substr(start + len + 1, 1) == 5) {
var flagval = oldstr.substr(start + len, 1) – 0;
if (flagval % 2 == 0) {
a1 = a1 – 1;
}
}
}
var b1 = a1 / Math.pow(10, len);
return b1;
}
Number.prototype.oldtoFixed = Number.prototype.toFixed;
Number.prototype.toFixed = function (len) {
var old = this;
var oldstr = old.toString()
var start = oldstr.indexOf(“.”);
if (len == 2 && start > 0 && oldstr.split(“.”)[1].length == 3) {
return this.round(len);
}
else {
return this.oldtoFixed(len);
}
}

    原文作者:银行家问题
    原文地址: https://www.cnblogs.com/yzryc/p/6053435.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞