완벽한 솔루션을 제공하는 것이 좋습니다하지만이 뒤에 이론을 배우고 싶은 경우에 당신에게 달려 아니에요하지만 다음에 무엇을 모르겠습니다.
public void comb(String A[], String str, int pos, int length)
{
if (length == 0) {
System.out.println(str);
} else {
for (int i = pos; i < A.length; i++)
comb(A, str + A[i], i + 1, length - 1);
}
}
public static void main(String[] args) {
String myArray[] = {"c", "a", "t", "d","o","g"};
C c = new C();
for (int i = 0; i <= myArray.length; i++)
c.comb(myArray, "", 0, i);
}
이 another answer에서는 유사한 생성기가 어떻게 작동하는지 간략하게 설명합니다.
Backtracking 및 조합 알고리즘에 대해서도 배우는 것이 좋습니다. 당신이 원하는 것은 예를 들어, 내가 생각할 수있는 유일한 방법은 이러한 모든 조합을 저장하기에 충분한 공간이 초기화되는 배열의 조합을 저장하는 경우는 :
편집
private String[] combinations;
private int count;
public void comb(String A[], String str, int pos, int length)
{
if (length == 0) {
combinations[count] = str;
count++;
} else {
for (int i = pos; i < A.length; i++)
comb(A, str + A[i], i + 1, length - 1);
}
}
public void solve()
{
// 64 = C(6, 6) + C(6, 5) + C(6, 4) + C(6, 3) + C(6, 2) + C(6, 1) + C(6, 0)
// where C(n, k) = binomial coefficient function.
combinations = new String[64];
count = 0;
String myArray[] = {"c", "a", "t", "d","o","g"};
for (int i = 0; i <= myArray.length; i++)
comb(myArray, "", 0, i);
for (int i = 0; i < combinations.length; i++)
System.out.println(combinations[i]);
}
public static void main(String[] args) {
C c = new C();
c.solve();
}
이인가 너의 숙제? – rendon
배열을 변경할 수 없기 때문에 arrayList를 사용하면 몇 가지 명백한 이점이있는 것으로 보입니다. – SeekingAlpha
@SeekingAlpha : 다시 살펴볼 수 있습니다. – user2357112