Java 使用递归打印所有数字序列
我的计算机算法课上有一个作业: 编写一个递归算法,给定一个正整数n>=1,它将打印所有 数列Java 使用递归打印所有数字序列,java,algorithm,recursion,Java,Algorithm,Recursion,我的计算机算法课上有一个作业: 编写一个递归算法,给定一个正整数n>=1,它将打印所有 数列 k>=1和1基本上,以n=5为例,您应该打印两种间隔 1 | full interval 1 2 | 1 2 3 | 1 2 3 4 | 1 2 3 4 5 | 1 _ 3 4 5 | concatenation of full interval (1 -> i) with (i+1 -> n) 1 2 _ 4 5 |
k>=1和1基本上,以
n=5
为例,您应该打印两种间隔
1 | full interval
1 2 |
1 2 3 |
1 2 3 4 |
1 2 3 4 5 |
1 _ 3 4 5 | concatenation of full interval (1 -> i) with (i+1 -> n)
1 2 _ 4 5 |
1 2 3 _ 5 |
1 _ _ 4 5 | concatenation of full interval (1 -> i) with (i+2 -> n)
1 2 _ _ 5 |
1 _ _ _ 5 | concatenation of full interval (1 -> i) with (i+3 -> n)
---------------------
2 | full interval
2 3 |
2 3 4 |
2 3 4 5 |
2 _ 4 5 | concatenation of full interval (1 -> i) with (i+1 -> n)
2 3 _ 5 |
2 _ _ 5 | concatenation of full interval (1 -> i) with (i+2 -> n)
---------------------
3 | full interval
3 4 |
3 4 5 |
3 _ 5 | concatenation of full interval (1 -> i) with (i+1 -> n)
---------------------
4 | full interval
4 5 |
5 | concatenation of full interval (breaks here)
然后,你要做的是:
1-打印从=1到=n的整个间隔
2-用“负”部分迭代连接两个间隔
3-再次调用递归方法传递(从++,到)
希望它有助于从一个集合
{a,b,c,…},你希望{a}和{c}递归地与{b,c,…}的所有子集的集合连接起来。从最右边的元素开始,一直到左边,直到达到1(递归的基本情况)。在基本情况下,返回“1”。
现在开始自下而上构建组合。函数foo
返回字符串的数组列表。在递归的每个级别上,有三个部分Part1
是上一次调用foo
返回的数组列表;在part2
中,将n
附加到上一次调用foo
返回的所有元素;最后,在part3
中,将n
附加到数组列表中
import java.util.*;
class Sequence
{
public static ArrayList<String> foo(int n)
{
if(n==1)
{
ArrayList<String> a = new ArrayList<String>();
a.add("1");
return a;
}
ArrayList<String> withOutN = foo(n-1);
ArrayList<String> out = new ArrayList<String>();
Iterator<String> it = withOutN.iterator();
Integer i = new Integer(n);
while(it.hasNext())
{
String s = it.next();
out.add(s);
s = s.concat("," + i.toString());
out.add(s);
}
out.add(i.toString());
return out;
}
public static void main(String[] args)
{
int n=4;
ArrayList<String> out = new ArrayList<String>();
out = (ArrayList<String>) foo(n);
Iterator<String> it = out.iterator();
while(it.hasNext())
{
System.out.println(( it.next()) );
}
}
}
import java.util.*;
类序列
{
公共静态数组列表foo(int n)
{
如果(n==1)
{
ArrayList a=新的ArrayList();
a、 添加(“1”);
返回a;
}
arraylistwithout n=foo(n-1);
ArrayList out=新的ArrayList();
迭代器it=withOutN.Iterator();
整数i=新整数(n);
while(it.hasNext())
{
字符串s=it.next();
out.添加(s);
s=s.concat(“,”+i.toString());
out.添加(s);
}
out.add(i.toString());
返回;
}
公共静态void main(字符串[]args)
{
int n=4;
ArrayList out=新的ArrayList();
out=(ArrayList)foo(n);
Iterator it=out.Iterator();
while(it.hasNext())
{
System.out.println((it.next());
}
}
}
请向我们展示您目前掌握的代码。我认为您的老师/教授/助教会是一个更好的寻求帮助的人。注意,良好做法:课程名称应该是CamelCase。我可以告诉您一些遗漏的案例,即1、2、4、5。这就是事情变得一团糟的地方。@Compass你说得对!还需要考虑间隔中被忽略的数字大小。固定的!
import java.util.*;
class Sequence
{
public static ArrayList<String> foo(int n)
{
if(n==1)
{
ArrayList<String> a = new ArrayList<String>();
a.add("1");
return a;
}
ArrayList<String> withOutN = foo(n-1);
ArrayList<String> out = new ArrayList<String>();
Iterator<String> it = withOutN.iterator();
Integer i = new Integer(n);
while(it.hasNext())
{
String s = it.next();
out.add(s);
s = s.concat("," + i.toString());
out.add(s);
}
out.add(i.toString());
return out;
}
public static void main(String[] args)
{
int n=4;
ArrayList<String> out = new ArrayList<String>();
out = (ArrayList<String>) foo(n);
Iterator<String> it = out.iterator();
while(it.hasNext())
{
System.out.println(( it.next()) );
}
}
}