将for循环转换为递归函数

时间:2013-05-13 16:10:00

标签: java

这是一个家庭作业问题。我无法将以下内容转换为递归函数:

public class Integrate {
    public static double integrate(int  a, int b, int steps)
    {
        double sum=0;
        double delta = 1.0 * (b - a)/steps;
        double x = a;
        double f = 0.5*x*x + 3*x + 5;

        for (int i = 0; i< steps; i++)
        {
            x = x + delta;
            double fr = 0.5*x*x + 3*x + 5;
            double area = f * delta + 0.5*(fr - f)*delta;
            sum += area;
            f = fr;
        }
        return sum;
    }
    public static void main(String [] args)
    {
        int a, b, step;
        a = Integer.parseInt(args[0]);
        b = Integer.parseInt(args[1]);
        step = Integer.parseInt(args[2]);
        System.out.format("Integral is %f\n", integrate(a,b,step));
    }
}

这是我到目前为止,但输出与原始代码不同。我无法弄清楚出了什么问题

public class Integrate {

    public static double integrate(int a, int b, int steps) {
        double sum=0;
        int i=0;
        sum = rintegrate(a, b, steps, i, sum);
        return sum;
    }

    public static double rintegrate(int a, int b, int steps, 
            int i, double sum) {
        double delta = 1.0 * (b - a)/steps;
        double x = a;
        double f = 0.5*x*x + 3*x + 5;
        if (i<steps) {
            x = x + delta;
            double fr = 0.5*x*x + 3*x + 5;
            double area = f * delta + 0.5*(fr - f)*delta;
            sum += area;
            f = fr;
            i++;
            rintegrate(a, b, steps, i, sum);
        }
        return sum;
    }

    public static void main(String[] args) {
        int a, b, step;
        a = Integer.parseInt(args[0]);
        b = Integer.parseInt(args[1]);
        step = Integer.parseInt(args[2]);
        System.out.format("Integral is %f\n", integrate(a,b,step));
    }

}

3 个答案:

答案 0 :(得分:3)

我不打算全面分析问题,但这里有一些观察结果

    if (i<steps) {
        x = x + delta;
        double fr = 0.5*x*x + 3*x + 5;
        double area = f * delta + 0.5*(fr - f)*delta;
        sum += area;
        f = fr;
        i++;
        rintegrate(a, b, steps, i, sum);
    }
    return sum;

sum += area;return sum;之间的所有内容都是多余的。

  • 您正在将f设置为fr,但之后您甚至都不会使用f。如果你希望下次f不同,也许你可以将它作为参数传递给你的递归函数
  • 你以递归方式调用rintegrate(...),但是你没有对它返回的值做任何事情。你可能想要使用那个值。

您应该考虑将递归视为使用较小版本的问题来解决自身。

以下是您的问题的一些代码,假设您有一个函数:segment只计算给定a的第一个细分的大小,delta

rintegrate(a, b, steps)
{
    if(steps <= 1)
    {
        delta = b-a;
        return segment(a, delta)
    }
    else
    {
        delta = (b-a)/steps
        return segment(a, delta) + rintegrate(a+delta, b, steps-1)
    }
}

答案 1 :(得分:1)

工作版

只需复制粘贴,您将获得与原始方法相同的输出。

   public static void main(String[] args) {
        int a = 1, b = 10, step = 1000;
            double delta = 1.0 * (b - a) / step;
        double sum = integrate(a, b, step, 0, 0, 0, delta);
        double test = working(a, b, step);
        System.out.println("Integral is " + sum);
        System.out.println("Integral is " + test);
    }

工作递归版:

    public static double integrate(double x, int b, int steps, int i,
            double sum, double f, double delta) { 
        f = 0.5 * x * x + 3 * x + 5;
        if (i < steps) {
            x = x + delta;
            double fr = 0.5 * x * x + 3 * x + 5;
            double area = f * delta + 0.5 * (fr - f) * delta;
            return integrate(x, b, steps, i + 1, sum + area, fr, delta);
        }
        return sum;
    }

您原来的迭代方法;

public static double working(int a, int b, int steps) {
    double sum = 0;
    double delta = 1.0 * (b - a) / steps;
    double x = a;
    double f = 0.5 * x * x + 3 * x + 5;

    for (int i = 0; i < steps; i++) {
        x = x + delta;
        double fr = 0.5 * x * x + 3 * x + 5;
        double area = f * delta + 0.5 * (fr - f) * delta;
        sum += area;
        f = fr;
    }
    return sum;
}

答案 2 :(得分:0)

这就是你想要的;)

public class Integrate{

    /**
     * @param args
     */
    public static void main(String[] args) {
        int a, b, step;
        a = Integer.parseInt(args[0]);
        b = Integer.parseInt(args[1]);
        step = Integer.parseInt(args[2]);
        System.out.format("Integral is %f\n",
                adaptiveSimpsons(a, b, step));

    }

    private static double f(double i) {
        return (0.5 * i * i + 3 * i + 5);
    }

    static double adaptiveSimpsons(double a, double b, // interval [a,b]
            int maxRecursionDepth) { // recursion cap
        double c = (a + b) / 2, h = b - a;
        double fa = f(a), fb = f(b), fc = f(c);
        double S = (h / 6) * (fa + 4 * fc + fb);
        return adaptiveSimpsonsAux(a, b, S, fa, fb, fc, maxRecursionDepth);
    }

    private static double adaptiveSimpsonsAux(double a, double b, double S, double fa,
            double fb, double fc, int bottom) {
        double c = (a + b) / 2, h = b - a;
        double d = (a + c) / 2, e = (c + b) / 2;
        double fd = f(d), fe = f(e);
        double Sleft = (h / 12) * (fa + 4 * fd + fc);
        double Sright = (h / 12) * (fc + 4 * fe + fb);
        double S2 = Sleft + Sright;
        if (bottom <= 0)
            return S2 + (S2 - S) / 15;
        return adaptiveSimpsonsAux(a, c, Sleft, fa, fc, fd, bottom - 1)
                + adaptiveSimpsonsAux(c, b, Sright, fc, fb, fe, bottom - 1);
    }
}

经过测试和工作

给出here

的翻译C代码