[英]Printing out a tower of numbers
I am trying to prepare for an exam, so I thought I'd go to the internet and find some recursion examples. 我正在尝试为考试做准备,所以我想我应该上网浏览一些递归示例。 Now the task I found was the following: You should code a printSequence method, where the output should like the following for n=3 现在我发现的任务如下:您应该编写一个printSequence方法,其中对于n = 3,输出应如下所示:
1 1个
12 12
123 123
12 12
1 1个
and for n = 5 并且对于n = 5
1 1个
12 12
123 123
1234 1234
12345 12345
1234 1234
123 123
12 12
1 1个
Now I am pretty new to the idea of recursion, so sorry for asking something for such a simple task. 现在,我对递归的想法还很陌生,因此很抱歉为这样一个简单的任务提出一些问题。 But I cannot get my head around on how to solve this. 但是我无法解决这个问题。 I managed to print something like this for n=3 我设法在n = 3的情况下打印了这样的内容
123 123
12 12
1 1个
but I just can't get a grasp on how to do the upper part. 但我只是无法掌握上半部分的用法。
public static int printSequence(int n){
if (n >= 1){
for (int i = 1; i <= n; i++)
System.out.print(i);
System.out.println();
return printSequence(n-1);
}
return 0;
}
It might be that my idea is completely wrong, but as I said, I just can't think of another way on how to do this. 可能是我的想法完全错误,但是正如我所说,我只是想不出另一种方法来做到这一点。
There is probably a cleaner way to do this, but simple way is to create another function and pass default parameters there: 可能有一种更简洁的方法来执行此操作,但是简单的方法是创建另一个函数并在其中传递默认参数:
i
is bigger than n
then you quit; 您检查当前迭代值i
是否大于n
然后退出; i
== n
you just print one string (the pinnacle or your pyramid); 如果i
== n
,则只打印一个字符串(尖峰或金字塔); For example: 例如:
public static int printRecursive(int n, String line, int i) {
if (i > n) {
return 0;
}
System.out.println(line);
if (i == n) {
return 0;
}
printRecursive(n, line + " " + (++i), i);
System.out.println(line);
return 0;
}
public static int printSequence(int n){
return printRecursive(n, "1", 1);
}
This seems to work. 这似乎有效。 Notes: 笔记:
printSequence
itself being recursive, it just accepts the limit (n) and acts as a wrapper to call the recursive method starting at 1. 而不是printSequence
本身是递归的,它仅接受限制(n)并充当包装器以调用从1开始的递归方法。 public static void main(String[] args) {
printSequence(5); // test value
}
private static void printSequence(int n) {
if (n < 1 || n > 9) {
throw new IllegalArgumentException("Argument must be in the range 1 to 9 inclusive.");
}
doRecursion(1, "", n); // call recursive method with initial values
}
private static void doRecursion(Integer counter, String currentString, int limit) {
String newString = currentString + counter.toString();
System.out.println(newString);
if (counter < limit) {
doRecursion(counter + 1, newString, limit);
System.out.println(newString);
}
}
This does exactly what you want: 这正是您想要的:
//This is the method that will be called from some other class
public static int printSequence(int n){
return printSequence(1, n);
}
/*We define this method, because the following code snippet
is used multiple times in the method 'printSequence(int, int)'
This method will simply print the numbers 1 - length*/
private static void printLooper (int length){
for(int i=1; i<=length; i++)
System.out.print(i);
System.out.print("\n");
}
/*This method has the logic. We give it 2 integers as parameters:
int start and int end. I think the names of the parameters are self-explanatory*/
private static int printSequence(int start, int end){
/*This is the TERMINATING condition, so it is placed first.
This part is really important. Always be aware of what type of recursion
you are using. More on this later..*/
if ( end == 1 ) {
printLooper(end);
return 0;
}
//OK. So, if start is less than end, then print 1 - start
if (start < end){
printLooper(start);
//then return method recursively, with start INCREMENTED by one
return printSequence(start+1, end);
}
/*Now, if start is equal to end, print number 1 - start, but this time,
return the method recursively with end DECREMENTED by one*/
else if (start == end){
printLooper(start);
return printSequence(start, end-1);
}
/*Now if start is larger than end, then that means that we need to start
printing the lower part of the 'number tree'. So, call printLooper()
to print 1 - end.*/
else if (start > end){
printLooper(end);
return printSequence(start, end-1);
}
return 0;
}
As for types of recursion and why it is important to know what type of function you are writing, check out this awesome tutorial . 至于递归的类型以及为什么要知道要编写的函数类型很重要,请查看此很棒的教程 。
Here's a code that might be of help to you. 这是一个可能对您有帮助的代码。 The code is as follows. 代码如下。 It's quite self-explanatory. 这是不言自明的。 But I will add a detailed explanation if it seems complicated to you. 但是,如果您觉得它很复杂,我将添加详细的解释。
public static int printSequence(int n) {
return printSequence(1, n); //start printing upper triangle (1 to n)
}
public static int printSequence(int currentNumber, int lastNumber){
for (int i = 1; i <= currentNumber; i++)
System.out.print(i);
System.out.println();
if(currentNumber<lastNumber) //if in upper triangle
return printSequence(currentNumber+1, lastNumber);
if(currentNumber>lastNumber) //if in lower triangle
return printSequence(currentNumber-1, lastNumber);
if(currentNumber==lastNumber) { //if the end of a triangle is reached
if(lastNumber==1) { //if the end of lower triangle is reached, exit the method
return 0;
} else {
return printSequence(lastNumber-1, 1); //if the end of upper triangle is reached, start the lower triangle ( n-1 to 1)
}
}
return 0;
}
I think this solution might work but it uses 2 functions. 我认为此解决方案可能有效,但它使用了2个功能。 Each function has recursion in it: 每个函数都具有递归:
public static void printSequence(int n){
printSequenceHelp1(1, n - 1);
for (int i = 1; i <= n; i++)
System.out.print(i);
System.out.println();
printSequenceHelp2(n - 1);
}
public static void printSequenceHelp1(int k, int n){
if (k <= n){
for (int i = 1; i <= k; i++)
System.out.print(i);
System.out.println();
printSequenceHelp1(k + 1, n);
}
}
public static void printSequenceHelp2(int n){
if (n >= 1){
for (int i = 1; i <= n; i++)
System.out.print(i);
System.out.println();
printSequenceHelp2(n - 1);
}
}
I am pretty sure there is a more elegant solution with only one function. 我敢肯定,只有一个功能有一个更优雅的解决方案。 I'll try to think of it when if I'll find it I'll post it here. 如果可以找到它,我会在这里发布。
A recursive solution 递归解决方案
public static int printSequence(int n,int count){
if(count == 2*n){
return 0;
}
else{
if(count<=n){
int i=1;
while(i<=count)
{
System.out.print(i);
i++;
}
System.out.println();
return printSequence(n,count+1);
}
else{
int i=1;
while(i<=n-(count-n))
{
System.out.print(i);
i++;
}
System.out.println();
return printSequence(n,count+1);
}
}
}
printSequence(n,1); printSequence(n,1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.