Need help converting iterative process to recursiv

2019-09-14 22:36发布

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();
    }

with the output:

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

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();

    }

}

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.

3条回答
再贱就再见
2楼-- · 2019-09-14 23:15

I would start by extracting then STAR and DASH,

private static final String DASH = "-";
private static final String STAR = "*";

Next, I would write a method to repeat a String a given number of times. Also, I would use a StringBuilder (here I've done it recursively)

private static StringBuilder repeat(StringBuilder sb, String str, int n) {
    if (n > 0) {
        sb.append(str);
        repeat(sb, str, n - 1);
    }
    return sb;
}

Next, a private recursive method to print the pattern based on StringBuilder

private static void printPattern(StringBuilder sb, int s) {
    System.out.println(sb);
    int p = sb.indexOf(DASH, s);
    if (p > -1) {
        sb.replace(p, p + DASH.length(), STAR);
        printPattern(sb, s + STAR.length());
    }
}

And finally the public method

public static void printPattern(int n) {
    printPattern(repeat(new StringBuilder(), DASH, n), 0);
}
查看更多
可以哭但决不认输i
3楼-- · 2019-09-14 23:23

First let m = number of '*' to print and let n = number of '-' to print

For each recursion, increment m by 1 and decrement n by 1.

public static void main(String[] args) {
    printPattern(3);
}

public static void printPattern(int n) {
    printing(n, n);
}

//Variable size basically represent the number of columns
public static void printing(int n, int size) {
    //stop condition
    if(n == -1)
        return;

    //m is the number of * to print
    int m = size - n;
    printAsterisk(m);

    //n is the number of - to print
    printHyphen(n);

    System.out.println();

    printing(n - 1, size);
}

public static void printAsterisk(int m) {
    if(m == 0)
        return;
    System.out.print('*');
    printAsterisk(m - 1);
}

public static void printHyphen(int n) {
    if(n == 0)
        return;
    System.out.print('-');
    printHyphen(n - 1);
}
查看更多
Summer. ? 凉城
4楼-- · 2019-09-14 23:23

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);
}

Worker is just an interface,

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();
    }
});
查看更多
登录 后发表回答