如何使用递归打印一个三角形?

huangapple go评论68阅读模式
英文:

How to print a triangle using recursion?

问题

我有一个任务,需要使用递归打印出一个三角形图案,如下所示:

*
**
***
****
***
**
*

通过调用 triangle(4) 生成。以下是我的代码:

public static void triangle(int height) {
    if (height == 0) {
        return;
    }
    triangle(height - 1);
    print_rowhelper(height, height - 1);
}

public static void print_row(int x) {
    for (int i = 0; i < x; i++) {
        System.out.print("*");
    }
    System.out.println();
}

public static void print_rowhelper(int x, int y) {
    print_row(x);
    for (int i = 0; i <= y - 1; i++) {
        y -= 1;
        System.out.print("*");
    }
    System.out.println();
}

我已经调整了变量,最好的结果是这样的...

*
**
***
****

... 当代码按照以下方式编写时:

public static void triangle(int height) {
    if (height == 0) {
        return;
    }
    triangle(height - 1);
    print_row(height);
}

当我尝试让它减少时,我遇到了难题。

英文:

I have an assignment where I need to print a triangle pattern using recursion that looks like so:

*
**
***
****
***
**
*

It is generated by calling triangle(4). Here is my code:

public static void triangle(int height) {
	if (height == 0) {
		return;
	}
	triangle(height - 1);
	print_rowhelper(height, height - 1);
}

public static void print_row(int x) {
	for (int i = 0; i &lt; x; i++) {
		System.out.print(&quot;*&quot;);
	}
	System.out.println();
}
	
public static void print_rowhelper(int x, int y) {
	print_row(x);
	for (int i = 0; i&lt;= y - 1; i++) {
		y -= 1;
		System.out.print(&quot;*&quot;);
	}
	System.out.println();
}

I've moved around variables and the best I've accomplished is this ...

*
**
***
****

... when the code is written in the following way:

public static void triangle(int height) {
	if (height == 0) {
		return;
	}
	triangle(height - 1);
	print_row(height);
}

I've hit a wall when I try to make it decrease.

答案1

得分: 1

你应该添加关于width变化方向的信息:+1-1。初始时inc = 1,当row == height时应更改为inc = -1

public static void triangle(int height) {
    triangle(1, 1, 1, height);
}

private static void triangle(int row, int width, int inc, int height) {
    if (width == 0)
        return;

    for (int i = 0; i < width; i++)
        System.out.print('*');

    System.out.println();

    if (row == height)
        inc = -1;

    triangle(row + 1, width + inc, inc, height);
}

输出:

triangle(4);
System.out.println();
triangle(5);

*
**
***
****
***
**
*

*
**
***
****
*****
****
***
**
*
英文:

You should add infromation about direction of width changing: +1 or '-1'. Initially inc = 1 and should be changed to inc = -1 when row == height.

public static void triangle(int height) {
    triangle(1, 1, 1, height);
}

private static void triangle(int row, int width, int inc, int height) {
    if (width == 0)
        return;

    for (int i = 0; i &lt; width; i++)
        System.out.print(&#39;*&#39;);

    System.out.println();

    if (row == height)
        inc = -1;

    triangle(row + 1, width + inc, inc, height);
}

Output:

triangle(4);
System.out.println();
triangle(5);
    
*
**
***
****
***
**
*

*
**
***
****
*****
****
***
**
*

答案2

得分: 0

我想再添加一个例子

```java
class Main {
	
	private static void triangle(int n, int i) {
		printStars(i);
		if (i < n) {
			triangle(n, i + 1);
			printStars(i);
		}
	}
	
	
	static void triangle(int n) {
		triangle(n, 1);
	}
	
	
	static void printStars(int n) {
		for (int i = 0; i < n; ++i) {
			System.out.print('*');
		}
		System.out.print('\n');
	}
	
	
	public static void main(String[] args) {
		triangle(4);
	}
}

关键是在两个打印语句之间使用递归。可以使用递归调用来打印三角形两侧的星号表示其深度。因此,我们在这里利用了递归调用被层次化的特点。


<details>
<summary>英文:</summary>

I&#39;d like to add another example:

```java
class Main {
	
	private static void triangle(int n, int i) {
		printStars(i);
		if (i &lt; n) {
			triangle(n, i + 1);
			printStars(i);
		}
	}
	
	
	static void triangle(int n) {
		triangle(n, 1);
	}
	
	
	static void printStars(int n) {
		for (int i = 0; i &lt; n; ++i) {
			System.out.print(&#39;*&#39;);
		}
		System.out.print(&#39;\n&#39;);
	}
	
	
	public static void main(String[] args) {
		triangle(4);
	}
}

The trick is to use recursion in-between two print-statements. You can use a recursive call to print its depth in form of stars for two sides of the triangle. So we are using the fact that recursive calls are layered here.

huangapple
  • 本文由 发表于 2020年9月21日 06:47:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63984354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定