我想为一个小游戏编写关卡系统。 等级系统将与分数 Hook ,等级将进一步上升 只给出了2个score值
lvl, 分数
0、50(从 0 - 50)
1、100(从 51 到 100)
2, 150
3、250
4、400
5、650
...
在给定分数和 2 个起始值(示例中为 50 和 100)的情况下,我如何优雅地计算出我所处的女巫等级
或者最好只计算列表或数组中的分值
最佳答案
如果没有任何公式,您可以简单地在瞬间计算出整个表格(在 core2 上不到 0.0002 秒)。对 int 求和非常快。在达到 int32 的最大值之前,这仅需要 36 次计算。
var scoreTable = new []{50, 100, 150, 250, 400, 650, 1050, 1700, 2750, 4450, 7200, 11650, 18850,
30500, 49350, 79850, 129200, 209050, 338250, 547300, 885550, 1432850, 2318400, 3751250,
6069650, 9820900, 15890550, 25711450, 41602000, 67313450, 108915450, 176228900,
285144350, 461373250, 746517600, 1207890850, 1954408450};
对于创建表格的数学,让我们简单点:
var thresholds = new List<int> {50, 100};
var index = 1;
while(true){
var temp = thresholds[cpt] + thresholds[cpt - 1];
if (temp < 0) break;
thresholds.Add(temp);
}
并了解级别:
var score = 51;
// Index is Zero-based numbering. Count is One-based. Index = Count -1;
var level = scoreTable.Where(x => x < score ).Count() - 1;
二进制搜索:
public class SupComparer : IComparer
{
public int Compare(object x, object y)
{
var t1 = UInt64.Parse(x.ToString());
var t2 = UInt64.Parse(y.ToString());
return t1.CompareTo(t2) > 0 ? 1 : 0;
}
}
var cp = new SupComparer();
var level = Array.BinarySearch(scoreTable, score, (IComparer)cp);
关于c# - Fibonacci 订单中的计算位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57370228/