[英]Printing sequence of numbers via recursion
I want to print following sequence of numbers 0 1 2 3 4 5 4 3 2 1 0
using recurstion我想使用递归打印以下数字序列
0 1 2 3 4 5 4 3 2 1 0
What i have tried so far produces following sequence 0 1 2 3 4 5
.到目前为止我所尝试的会产生以下序列
0 1 2 3 4 5
。 What i can't figure out is how to print remaining sequence.我不知道如何打印剩余序列。
How can i achieve the desired output?我怎样才能达到所需的 output?
Code代码
class Main {
public static void foo(int num) {
if (num == 0) {
System.out.print(num + " ");
return;
}
foo(num - 1);
System.out.print(num + " ");
}
public static void main(String[] args) {
Main.foo(5);
}
}
Here's a possible way to do it.这是一种可能的方法。 In a lot of cases with recursion you might find that introducing a "helper" method that the original method calls can help you achieve what you want when the original with just that one argument
num
might make it difficult to:在许多使用递归的情况下,您可能会发现引入原始方法调用的“辅助”方法可以帮助您实现所需的目标,而仅使用一个参数
num
的原始方法可能难以:
public class Main
{
public static void foo(int num) {
helper(num, 0);
}
private static void helper(int num, int count) {
if (count == num) {
System.out.print(count + " ");
} else {
System.out.print(count + " ");
helper(num, count + 1);
System.out.print(count + " ");
}
}
public static void main(String[] args) {
Main.foo(5);
}
}
NO need to using iteration
number无需使用
iteration
次数
public static void print(int num) {
if (num >= 5) {
System.out.print(num + " ");
return;
}
System.out.print(num + " ");
print(num + 1);
System.out.print(num + " ");
}
public static void main(String[] args) {
print(0);
}
, output , output
0 1 2 3 4 5 4 3 2 1 0
public static void recursion(int lo, int current, int hi){
if(current != hi){
System.out.printf("%d ", current);
recursion(lo, current+1, hi);
}
System.out.printf("%d%c", current, current == lo ? '\n' : ' ');
}
This method can print out an string of numbers like yours.这种方法可以打印出像你这样的一串数字。 It needs the starting point (lo), the current position in the list(current), and the maximum number(hi).
它需要起点(lo),列表中的当前position(current)和最大数量(hi)。
The recursive base case(when we don't want to use recursion anymore) is when current == hi
.递归基本情况(当我们不想再使用递归时)是
current == hi
。 We print out the number, then return.我们打印出号码,然后返回。
All other values print current, recurse, then print again after recursion is done.所有其他值打印当前,递归,然后在递归完成后再次打印。
This solution has the added benefit of printing a space after each char except after the final number.该解决方案的另一个好处是在每个字符之后打印一个空格,除了最后一个数字之后。 No trailing space:)
没有尾随空格:)
Initial invocation of this method would be like so:此方法的初始调用如下所示:
recursion(0, 0, 5); //prints 0 -> 5 -> 0
recursion(0, 5, 5); //prints 5 -> 0
recursion(0, 1, 5); //prints 1 -> 5 -> 0
recursion(0, 6, 5); //counts up forever and eventually crashes
Some helpful method to make calling simpler for common uses, you could do something like this:一些有用的方法可以使调用更简单以用于常见用途,您可以执行以下操作:
public static void countUpFromZeroDownToZero(int max){
recursion(0, 0, max);
}
public static void countFromXToYToX(int lo, int hi){
recursion(lo, lo, hi);
}
Not exactly clean, but gets the job done:不完全干净,但可以完成工作:
public static void main(String[] args) {
printPattern(5);
}
public static void printPattern(int stop) {
printPattern(0, stop, 1, false);
}
public static void printPattern(int n, int stop, int dir, boolean turnedAround) {
System.out.println(n);
if (n == stop) {
if (turnedAround)
return;
printPattern(n - dir, 0, -dir, true);
} else
printPattern(n + dir, stop, dir, turnedAround);
}
One solution can be the following:一种解决方案可能如下:
public class Main {
public static void main (String[] args) {
Main.foo(5, 5);
}
public static void foo(int num, int th) {
if (num == 0) {
System.out.print(num + " ");
return;
}
if ( num == 1) {
System.out.print(th + " ");
System.out.print((th - num) + " ");
return;
}
System.out.print(( th - num) + " ");
foo(num - 1, th);
System.out.print((th - num) + " ");
}
}
Here is an IDEONE link https://ideone.com/9U44NX这是一个 IDEONE 链接https://ideone.com/9U44NX
To respond just to your question, you can do it like this.要回答您的问题,您可以这样做。
Call it叫它
foo(0);
And the method以及方法
public static void foo(int a) {
if (a > 5) {
return;
}
prints 0 thru 5 ascending
System.out.println(a);
// once 5 is printed, don't print it again)
if (a == 5) {
return;
}
// bump a by 1
foo(a+1);
// A the calls to foo return the older versions of
// the variable `a` reappear from the stack. But 5
// was skipped. So it's just
//printing 4 thru 0 descending
System.out.println(a);
}
Alternative method (slight different calling with a termination value included)替代方法(包含终止值的略有不同的调用)
public static void main(String[] args) {
foo(5,5);
}
public static void foo(int a, int b) {
System.out.println(b-a);
if (a != 0) {
foo(a-1,b);
System.out.println(b-a);
}
}
It could be invoked as follows to more closely meet your requirement.可以按如下方式调用它以更接近您的要求。
foo(5);
public static void foo(int b) {
foo(b,b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.