半衰期。需要帮助设计算法

时间:2016-05-04 22:42:43

标签: algorithm

输入:

x - how many days patient was taking a substance
y - how many miligrams per day
z - what's the half-life of the substance in hours

输出:

a - for how many hours substance will stay in patient's body
    (Let's assume that below y/200 threshold we say it's gone).

编辑: 到目前为止,我所知道的是不正确的。在这里:

public static int computeEliminationTime(int days, double dosage, int half_life) {
    double saturation = 0.0d;
    double threshold = dosage/200.0d;
    boolean isHeStillTakingMedicine = true;
    for (int hours = 0;; hours++) {
        if (isHeStillTakingMedicine) {
            if (hours % 24 == 0) {
                saturation += dosage;
                days--;
                if (days == 0){
                    isHeStillTakingMedicine = false;
                    hours=1;
                }
            }
        }

        if (hours % half_life == 0) {
            saturation /= 2.0d;
        }
        if (saturation < threshold) {
            return hours;
        }
    }
}

我知道这种方法是错误的,因为消除某种物质是一个连续的过程,它不会在每个半小时的生命周期内发生。我研究了一下exponential decay,我只是不知道如何处理这种怪物。我的数学技能很差。

2 个答案:

答案 0 :(得分:1)

首先让我们看看物质在一天内衰变了多少。当超过 z 小时时,数量减少1/2倍,然后超过24小时减少 q =(1/2) 24 / z = 2 -24 / z 次。

现在让我们看看服用最后一剂后会有多少物质。在第一次剂量后,它 T 1 = y 。在 n + 1天,金额为 T n +1 = T n q + y 。我们需要在 x 日找到物质 T x 的数量。

如果你试着写出这个递归关系的前几个术语,你会发现它归结为 T x = y q x -1 + y q < em> x -2 + ... + y 。这只是几何级数的总和。因此, T x = y •(1 - q x )/(1 - q

现在我们必须找出 T x 衰减到 y / 200所需的时间。那是日志 2 T x /( y / 200))半衰期,即等于 z 日志 2 T x /( y / 200))小时。

因此,如果您替换 T x ,结果将是 a = z 记录 2 (200•(1 - q x )/(1 - q ))小时,其中 q = 2 -24 / z

答案 1 :(得分:0)

这是我最终提出的:

public static int computeEliminationTime(int days, double dosage, int half_life) {
    double saturation = 0.0d;
    double threshold = dosage/200.0d;
    double degradationFactor = Math.pow(2, -1.0d/(double)half_life);
    boolean isHeStillTakingMedicine = true;
    for (int hours = 0;; hours++) {
        if (isHeStillTakingMedicine) {
            if (hours % 24 == 0) {
                saturation += dosage;
                days--;
                if (days == -1){
                    isHeStillTakingMedicine = false;
                    hours=0;
                }
            }
        }

        saturation = saturation * degradationFactor; 

        if (saturation < threshold) {
            return hours;
        }
    }
}