在C#中反转这个数学表达式

时间:2016-06-30 20:26:22

标签: c# math

我对这篇文章有很多不喜欢的地方,我不知道为什么,但是为了让你们帮我解决这个问题,我会把这个剧本作为礼物给你。此脚本将经验转换为级别,并从给定指数表达式的级别转换为经验点。这些常数确保100级将等于1000万经验。在Runescape中,它们的等级99等于13,032,xxx是一个奇怪的数字。

using System.IO;
using System;

class Program
{
const float salt = 2.82842712474619f;
const float factor = 0.64977928f;
const int lvl_100_XP = 10000000;

static void Main()
{
    int xp = 9717096;// lvl 99
    int lvl = ExperienceToLevel(xp);
    Console.WriteLine("LVL: " + lvl.ToString()+ " XP: " + LevelToExperience(lvl).ToString());
}
static public int ExperienceToLevel(int xp){
    int lvl = 0;
    if (xp == lvl_100_XP){//9999987 is lvl 100 due to roundoff issues so it is fixed to 10mill
        lvl = 100;
    }
    else{
        lvl = (int)((1f / salt) * (float)Math.Pow((float)xp, (1f - factor)));
        if (lvl == 0){
            //lvl = 1;
        }
    }
    if (lvl == 100 && xp < lvl_100_XP){
           lvl = 99;
    }
    return lvl;
}

static public int LevelToExperience(int lvl){
    int xp = 0;
    if (lvl == 100){//9999987 is lvl 100 due to roundoff issues so it is fixed to 10mill
        xp = lvl_100_XP;
    }
    else{

        xp = (int)Math.Exp((float)Math.Log(salt * (float)lvl) / (1f - factor))+1;

        if (xp <= 1){
            xp= 0;
        }
        if (lvl == 100){
            xp = lvl_100_XP;
        }
    }
    return xp;
}
}

2 个答案:

答案 0 :(得分:6)

让我们解决它。

x成为体验,ac是常量。 L是级别。我们将取幂标记为^;请注意,C#没有这样的运算符。 C#中的^是异或。

你有

b = x / a
d = x ^ c
L = b / d

所以

L = x / (a * x ^ c)

L = (1 / a) * (x / x ^ c)

L = (1 / a) * x ^ (1 - c)

你希望解决x。因此,将两边乘以'a':

a * L = x ^ (1 - c)

取双方的ln。 (或者你最喜欢的任何对数。)

ln (a * L) = (1 - c) ln (x)

将双方除以1 - c

ln (a * L) / (1 - c) = ln x

消灭ln;记住exp是ln的倒数。如果您使用了其他一些对数,那么请使用其他指数。

exp (ln (a * L) / (1 - c)) = x

我们已经完成了。

答案 1 :(得分:0)

首先,exp到level是多对一关系。相反的是一对多,那么你想要在lvl2exp中返回什么exp?该级别的最低要求?最大值?平均值?

我认为你最好只计算一个带有每个级别经验括号的表(如果它不会改变就将其缓存到文件中),并简单地进行二进制搜索以找到任何给定级别的相应括号。 / p>