Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.daporkchop;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.stream.IntStream;
- public class Aufgabe7f {
- public static final int SAMPLES = 10_000;
- public static final int CARDS = 597;
- public static final int GROUP_SIZE = 3;
- public static final ThreadLocal<int[][]> TL_DECKS = ThreadLocal.withInitial(() -> new int[GROUP_SIZE][CARDS]);
- public static final ThreadLocal<int[]> TL_COUNTS = ThreadLocal.withInitial(() -> new int[GROUP_SIZE]);
- public static void main(String[] args) {
- double avgCount = IntStream.range(0, SAMPLES).parallel()
- .map(_i -> {
- Random r = ThreadLocalRandom.current();
- int[][] decks = TL_DECKS.get();
- for (int[] deck : decks) {
- Arrays.fill(deck, 0);
- }
- int[] counts = TL_COUNTS.get();
- Arrays.fill(counts, 0);
- int finishedDecks = 0;
- int purchasedCount = 0;
- while (finishedDecks < GROUP_SIZE) {
- for (int player = 0; finishedDecks < GROUP_SIZE && player < GROUP_SIZE; player++) {
- if (counts[player] == CARDS) {
- continue;
- }
- int card = r.nextInt(CARDS);
- if (decks[player][card]++ == 0) { //we've drawn the card for the first time
- if (++counts[player] == CARDS) {
- finishedDecks++;
- }
- } else { //we already have the card, attempt to trade
- TRADE:
- for (int p = 0; p < GROUP_SIZE; p++) { //search for a player we can trade with
- if (p == player || counts[p] == CARDS || decks[p][card] != 0) {
- continue;
- }
- for (int c = 0; c < CARDS; c++) { //search for a card they can give us
- if (decks[player][c] == 0 //we don't have the card
- && decks[p][c] > 1) { //they have multiple instances of the card
- decks[player][c] = 1;
- decks[p][c]--;
- decks[p][card] = 1;
- if (++counts[player] == CARDS) {
- finishedDecks++;
- }
- if (++counts[p] == CARDS) {
- finishedDecks++;
- }
- break TRADE;
- }
- }
- }
- }
- purchasedCount++;
- }
- }
- return purchasedCount;
- })
- .average().orElseThrow();
- System.out.printf("purchased an average of %.2f cards (%.2f cards per person)\n", avgCount, avgCount / GROUP_SIZE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement