여기 방법이 있습니다.
public static void choosePatterns(int total, int toChoose) {
List<Integer> indices = IntStream.range(1, toChoose + 1).mapToObj(i -> Integer.valueOf(0)).collect(Collectors.toList());
resetIndex(indices, 0, 1, total);
while (true) {
System.out.println("chosen: " + indices);
System.out.print("not chosen: ");
for (int i = 1; i <= total; i++) {
if (! indices.contains(Integer.valueOf(i))) {
System.out.print(i + " ");
}
}
System.out.println("\n");
if (! incrementIndices(indices, indices.size() - 1, total)) {
break;
}
}
}
public static boolean resetIndex(List<Integer> indices, int posn, int value, int total) {
if (value <= total) {
indices.set(posn, value);
return posn == indices.size() - 1 ? true : resetIndex(indices, posn + 1, value + 1, total);
} else {
return false;
}
}
public static boolean incrementIndices(List<Integer> indices, int posn, int total) {
if (indices.get(posn) < total) {
indices.set(posn, indices.get(posn) + 1);
} else {
int resetPosn = posn;
do {
if (resetPosn-- == 0) return false;
} while (! resetIndex(indices, resetPosn, indices.get(resetPosn) + 1, total));
}
return true;
}
public static void main(String[] args) throws IOException {
choosePatterns(6, 2);
}
그것은이 인쇄 :
chosen: [1, 2]
not chosen: 3 4 5 6
chosen: [1, 3]
not chosen: 2 4 5 6
chosen: [1, 4]
not chosen: 2 3 5 6
chosen: [1, 5]
not chosen: 2 3 4 6
chosen: [1, 6]
not chosen: 2 3 4 5
chosen: [2, 3]
not chosen: 1 4 5 6
chosen: [2, 4]
not chosen: 1 3 5 6
chosen: [2, 5]
not chosen: 1 3 4 6
chosen: [2, 6]
not chosen: 1 3 4 5
chosen: [3, 4]
not chosen: 1 2 5 6
chosen: [3, 5]
not chosen: 1 2 4 6
chosen: [3, 6]
not chosen: 1 2 4 5
chosen: [4, 5]
not chosen: 1 2 3 6
chosen: [4, 6]
not chosen: 1 2 3 5
chosen: [5, 6]
not chosen: 1 2 3 4
출처
2016-09-09 11:54:34
mab
뭔가를이 작은, 당신을 위해, println 메소드 후 다른 루프를 추가 할 수 있습니다 (INT의 K = 0; K <6; 케이 ++) {((K 경우! = i) 및 (k = j)) {... println (... k ...)}}. println이 i 대신 data [i]와 같은 것을 사용해서는 안됩니까? – rcgldr
@rcgldr (+1), 나는 전혀 생각하지 못했습니다. 수락 할 수 있도록 답변으로 게시 해 주시겠습니까? 그리고 네, 그것은 데이터이어야합니다 [i]. 그러나 나는이 경우에 그것이 인쇄 될 때 실제로 차이를 말할 수 없다고 생각한다. –
_ 6에서부터 2_를 선택하면 _6과 거의 같아야합니다. 4_를 선택하십시오. – greybeard