1

我正在尝试编写一个算法,该算法将采用一组字母和一个包含单词大小的 int 并为此创建所有可能的字母分组,但我完全陷入困境......

例子:

**Input**
Letters: E
Size: 4

**Output** 
E___
E_E_
_E_E
EE__
_EE_
___E

etc..

另一个例子

**Input**
Letters: E, A
Size: 4

**Output** 
EA__
AE__
E_A_
_E_A
A_E_
etc...
4

1 回答 1

0

这是一个使用递归来解决问题的算法: 定义一个以长度、字符数组(包括 '_' )和另一个字符串数组作为参数的方法。

String[] meth(int l;char ch[],String[] s)
Define a new String array (ns) which contains N elements where N=length of array s * l
Copy each element of s into ns thrice, each time appending an element of ch
Make a recursive call to meth with (l, ch, ns) if the length of ns is less than l


If the input is {'E', 'A'} and 4
call meth(4,new char[]{'E','A','_'},new String[]{} )
于 2012-08-12T15:59:28.650 回答