[英]Java Number Pattern Recursion
I am working on a lab for a class where a user inputs a number and it recursively prints out a number pattern. 我正在一个班级的实验室中工作,用户在其中输入数字,然后递归地打印出数字模式。 For example,
例如,
The base case is if they enter 1, it will print: 1
基本情况是,如果输入1,则将打印:
1
If they enter 2 it will print: 1 2 1
如果他们输入2,它将打印:
1 2 1
If 3, it will print: 1 2 1 3 1 2 1
如果为3,它将打印:
1 2 1 3 1 2 1
and then for something bigger, if they enter 7, it will print: 然后输入更大的值(如果输入7),则会打印:
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
I'm a little stuck on what the number pattern is to be able to complete this problem. 我对如何完成此问题的数字模式有些困惑。 Does anyone have any ideas?
有人有什么想法吗?
So you need to write a recursive function. 因此,您需要编写一个递归函数。 Something of this form:
这种形式的东西:
private String pattern(int num) {
// ...
}
The most important part is finding the right exit condition that should stop the recursion. 最重要的部分是找到应该停止递归的正确退出条件。 In this case, that's when
num == 1
. 在这种情况下,那就是
num == 1
。
From the description, it looks like for a number k
, the output is pattern(k - 1) + k + pattern(k - 1)
. 根据描述,对于数字
k
,输出为pattern(k - 1) + k + pattern(k - 1)
。
I already spoiled too much. 我已经宠坏了。 You might need to improve the efficiency of this.
您可能需要提高效率。 For example, realize that you don't need to run
pattern(k - 1)
twice, it's enough to do it once. 例如,意识到您不需要运行
pattern(k - 1)
两次,一次就足够了。
I'm a little stuck on what the number pattern is to be able to complete this problem.
我对如何完成此问题的数字模式有些困惑。
Lets try to analyse the sequence using some function f 让我们尝试使用一些函数f分析序列
f(1) = 1 (Total digits = 1 ) f(1)= 1(总位数= 1 )
f(2) = 1 2 1 ( Total digits = 3 ) f(2)= 1 2 1(总位数= 3 )
f(3) = 121 3 121 (Total digits = 7 ) f(3)= 121 3 121(总位数= 7 )
f(4) = 1213121 4 1213121 (Total digits = 15 ) f(4)= 1213121 4 1213121(总位数= 15 )
f(5) = 121312141213121 5 121312141213121 (Total digits = 31 ) f(5)= 121312141213121 5 121312141213121(总位数= 31 )
So as you can observe total digits sequence looks like 1,3,7,15,31,....2^n-1 因此,您可以观察到总数字序列看起来像1,3,7,15,31,.... 2 ^ n-1
Now we can express this logic as mentioned below(Note : in order to help you to better understand how the program works i am printing sequence at every level) 现在,我们可以如下所述表达这种逻辑(注意:为了帮助您更好地理解程序的工作原理,我在每个级别上打印顺序)
public class SequenceGenerator {
public static void main(String[] args) {
generate(7);
}
static void generate(int depth) {
recursiveGenerator(1, null, depth);
}
static void recursiveGenerator(int num, String prev, int limit) {
if (num <= limit) {
if (prev != null) {
System.out.println();
}
if (prev != null) {
System.out.printf("%s %d %s", prev, num, prev);
} else {
prev = "";
System.out.printf("%d", num);
}
if (prev.equals("")) {
prev += num + prev;
} else {
prev += " " + num + " " + prev;
}
recursiveGenerator(++num, prev, limit);
}
}
}
Outputs 输出
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.