Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm trying to solve this assignment, too, and think it should be something like this:</p> <p>The number of players (=N), the number of unknown cards (count the "-1") and the sum of the known cards (count all cards except "-1") are given. The total number of games possible should be 1 +2 +3 + ... + (players-1): The first player has (players-1) opponents, the second player (players-2) etc.</p> <p>Now you can recursively calculate the sum of possible score cards:</p> <p>Initialize an empty hashmap with (players, unknown cards, sum of known cards) as the key and the sum of possible score cards as the value.</p> <p>If all cards are defined, then the answer is either 0 (if the sum of all cards equals the total number of games possible) or 1 (if the sum of all cards does not equal the total number of games possible).</p> <p>If not all cards are defined, then run a for loop and set one unknown card to 0, 1, 2 ... (players-1) and try to read the result from the hashmap. If it is not in the hashmap call the method itself and save the result in the map.</p> <p>The recursion code should be something like this:</p> <pre><code>def recursion(players: Int, games: Int, unknownCards: Int, knownScore: Int): Int = { unknownCards match { case 0 if knownScore != games =&gt; 0 case 0 if knownScore == games =&gt; 1 case _ =&gt; map.get(players, unknownCards, knownScore) getOrElse { var sum = 0 for (i &lt;- 0 until players) sum += main(players, games, unknownCards - 1, knownScore + i) sum %= 1000000007 map.put((players, unknownCards, knownScore), sum) sum } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload