英文:
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 < 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();
}
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 < width; i++)
System.out.print('*');
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'd like to add another example:
```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);
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论