Created
May 14, 2021 20:21
-
-
Save doruchiulan/987e4c8af13dd8b20da725e2ee5fb0ff to your computer and use it in GitHub Desktop.
allcombinations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.BitSet; | |
import java.util.*; | |
import java.util.stream.*; | |
public class A { | |
private static final int NUM_CARDS = 6; | |
private static final List<String> cardss = new ArrayList<>( | |
List.of("1399","1399","1399","69","69","69")); | |
public static void main(String[] args) { | |
enumerateAllHands(6); | |
} | |
private static void enumerateAllHands(int n) { | |
if (n > NUM_CARDS) { | |
throw new IllegalArgumentException(); | |
} | |
int[] cards = new int[n]; | |
BitSet cardsUsed = new BitSet(); | |
enumerate(cards, 0, cardsUsed); | |
} | |
private static void enumerate(int[] cards, int from, BitSet cardsUsed) { | |
if (from == cards.length) { | |
emit(cards); | |
} else { | |
for (int i = 0; i < NUM_CARDS; i++) { | |
if (!cardsUsed.get(i)) { | |
cards[from] = i; | |
cardsUsed.set(i); | |
enumerate(cards, from + 1, cardsUsed); | |
cardsUsed.clear(i); | |
} | |
} | |
} | |
} | |
private static void emit(int[] cards) { | |
Arrays.stream(cards) | |
.boxed() | |
.map(c -> cardss.get(c)) | |
.forEach(System.out::print); | |
// .collect(Collectors.toList()); | |
// System.out.println(Arrays.toString(cards)); | |
//List<?> aa = Arrays.asList(cards); | |
// System.out.println(list.get(0)); | |
//Arrays.asList(cards).stream().map(c -> cardss.get(c)) | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment