在java中将两个相互递归的方法转换为单个递归方法?

时间:2015-07-10 08:27:00

标签: java recursion mutual-recursion

我需要使用两个相互递归的方法来修改程序,以便它包含一个递归方法。根据我的理解,我需要通过将递归调用按照调用它们的顺序放在单个方法中来组合两个递归方法。问题是通过方法传递了4个整数,第一个方法调用第二个方法两次,第二个方法调用第一个方法两次。

这是原始代码:

public void drawHorizontal(Graphics graphics, double xMid, double yMid, double length )
{
    // find left endpoint
    double x1 = xMid - (length / 2);
    double y1 = yMid;

    // find right endpoint
    double x2 = xMid + (length / 2);
    double y2 = yMid;

   if (length > 5)
    {
    // draw a line from (x1,y1) to (x2,y2)
    graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

    // draw a vertical line with left end of horizontal as  midpoint of new line
    drawVertical(graphics, x1, y1, (length) );

    // draw a vertical line with right endof horizontal as  midpoint of new line
    drawVertical(graphics, x2, y2, (length) );
    }     

} // end drawHorizontal()

public void drawVertical(Graphics graphics, double xMid, double yMid, double length )
{
    // find upper endpoint
    double x1 = xMid; 
    double y1 = yMid - (length / 2);

    // right lower endpoint
    double x2 = xMid;
    double y2 = yMid + (length / 2);


   if (length > 5)
    {
    // draw a line from (x1,y1) to (x2,y2)
    graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

    // draw a 1/2 size horizontal line with top end of vertical as  midpoint of new line
    drawHorizontal(graphics, x1, y1, (length/2) );

    // draw a 1/2 horizontal line with bottom end of vertical as  midpoint of new line
    drawHorizontal(graphics, x2, y2, (length/2) );
    }

} // end drawVertical()

以下是我最近修改过的代码。我知道它的丑陋,但我无法弄清楚如何相互独立地调整x和y坐标。我尝试通过创建更多变量来解决这个问题,但我无法帮助,但我觉得我只是在做更多的工作。我能找到的最接近的堆栈问题是this。我从11岁开始就参与其中,现在是4:15。非常感谢正确方向的推动,谢谢你的时间。

编辑*感谢您的快速回复,我很感激。我知道以这种方式打破相互递归的方法似乎是违反直觉的,但我是编程和java的新手,所以我正在探索解决问题的不同方法。这是我最终打破它,它运行良好。谢谢你的时间。

修改后的代码:

    public void Total(Graphics graphics, boolean type, double xMid, double yMid, double length) {

    double x1;
    double y1;

    // find right endpoint
    double x2;
    double y2;

    if (type == false) {
        // find left endpoint
        x1 = xMid - (length / 2);
        y1 = yMid;

        // find right endpoint
        x2 = xMid + (length / 2);
        y2 = yMid;

        if (length > 5) {
            // draw a line from (x1,y1) to (x2,y2)
            graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

            // draw a vertical line with left end of horizontal as  midpoint of new line
            Total(graphics, true, x1, y1, (length));

            // draw a vertical line with right endof horizontal as  midpoint of new line
            Total(graphics, true, x2, y2, (length));
        }
    } else {

        // find upper endpoint
        x1 = xMid;
        y1 = yMid - (length / 2);

        // right lower endpoint
        x2 = xMid;
        y2 = yMid + (length / 2);

        if (length > 5) {
            // draw a line from (x1,y1) to (x2,y2)
            graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

            // draw a 1/2 size horizontal line with top end of vertical as  midpoint of new line
            Total(graphics, false, x1, y1, (length / 2));

            // draw a 1/2 horizontal line with bottom end of vertical as  midpoint of new line
            Total(graphics, false, x2, y2, (length / 2));
        }
    }
} 

1 个答案:

答案 0 :(得分:1)

您的请求很奇怪,相互递归通常有助于阅读...但是,确实存在(至少)一种通用方法。如果你有:

f(f_params) {
  …
  g(g_params…);
  …
}
g(g_params) {
  …
  f(f_params…);
  …
}

可以像这样转换:

fg(what,f_params,g_params) {
  if (what==FUNC_F) {
    …
    fg(FUNC_G,f_params,g_params);
    …
  } else if (what==FUNC_G) {
    …
    fg(FUNG_F,f_params,g_params);
    …
  }
}

如果你需要管理不同的返回类型,它会有点复杂:只需添加一些与返回值/类型相对应的输出参数。