需要帮助将迭代过程转换为递归过程 [英] Need help converting iterative process to recursive

查看:90
本文介绍了需要帮助将迭代过程转换为递归过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换以下迭代代码:

I am trying to convert the following iterative code:

int rows = 3;

    for (int i = 0; i <= rows; i++) 
    {
        for (int j = 0; j < i; j++)
        {
            System.out.print("*");
        }

        for (int j = 0; j < rows-i; j++)
        {
            System.out.print("-");
        }


        System.out.println();
    }

输出:

---
*--
**-
***

递归代码。这是一项任务。我创建了迭代代码,希望能够弄清楚如何将其直接转换为递归。这是我的努力:

to recursive code. This is for an assignment. I created the iterative code in hopes of being able to figure out how to directly convert it to recursive. Here's my effort of that:

public void stringY(int star, int count){
        if (star > 0){
            System.out.print("*");
            stringY(star - 1, count);
        }
}

public void stringX(int dash,int count){
    if (dash == -1) {
        return;
    }else if (dash < count){
        System.out.print("-");
        stringX(dash - 1, count);
    } else if (dash == count){
        stringX(dash - 1, count);
    }
}


public void printPattern(int n) {
    if (n == -1){
        return;
    } else {
        printPattern(n-1);
        stringY(n, n);
        stringX(n, n);
        System.out.println();

    }

}

我的问题在这里就是当我得到关于模式的*部分的输出时,我完全不知道如何获得模式的 - 部分。现在,这是一项任务,我不想要任何解决方案,但任何正确方向的指针都是绝对受欢迎的。我应该注意到我的两个要求是:1)我必须完全完成我的作业而不使用循环和2)我可以使用尽可能多的帮助方法,但主调用方法(printPattern)必须保持公共无效并且必须继续只接受整数。进一步说明:递归代码块中的另外两个方法是我创建的辅助方法。

My issue here is that while I get the output I am looking for with regard to the "*" part of the pattern, I have absolutely no clue how to get the "-" part of the pattern. Now being that this is an assignment I don't want any solutions, but any pointers in the right direction are absolutely welcome. I should note that my two requirements are: 1) I have to complete my assignment entirely without using loops and 2) I can use as many helper methods as I need, but the main calling method (printPattern) must stay public void and must continue to only accept integers. Further clarification: The other two methods in the recursive code block are helper methods I created.

推荐答案

这样想,它们都是循环做一些工作。所有你需要的理论上是一个递归函数,它调用自己直到传递的值。

Think of it this way, they are all just loops doing some work. All you need is theoretically one recursive function that calls itself till the passed value.

void loop(int i, int till, Worker doThis) {
    if (i>=till) return;
    doThis.work(i);
    loop(i+1, till, doThis);
}

工人只是一个界面,

public interface Worker {
    void work(int index);
}

现在我们需要传递需要完成的工作。有三个循环,因此三次调用循环函数。

Now we need to pass the work that needs to be done. There are three loops, hence three calls to the loop function.

final int rows = 3;
// outer loop
loop(0, rows+1, new Worker() {
    public void work(int index) {
        // Stars
        loop(0, index, new Worker() {
            public void work(int index) {
                System.out.print("*");
            }
        });
        // Dashes
        loop(0, rows-index, new Worker() {
            public void work(int index) {
                System.out.print("-");
            }
        });
        System.out.println();
    }
});

这篇关于需要帮助将迭代过程转换为递归过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆