Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Dice Dynamic Pro (not simple)
Forum - C/C++ - Dice Dynamic Pro (not simple)

Avatar
Car4m (Normal User)
Newbie


Messaggi: 7
Iscritto: 19/06/2015

Segnala al moderatore
Postato alle 0:10
Martedì, 30/06/2015
Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X.
X is the summation of values on each face when all the dice are thrown.

With the following code we return all the permutations =(
But we are looking for just one permutations of each result: 4 and 3 is the same of 3 and 4 (with two dices and sum 7)
Codice sorgente - presumibilmente C++

  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. int findWays(int m, int n, int x){
  6.     int table[n + 1][x + 1];
  7.     memset(table, 0, sizeof(table));
  8.  
  9.     for (int j = 1; j <= m && j <= x; j++)
  10.         table[1][j] = 1;
  11.  
  12.     for (int i = 2; i <= n; i++)
  13.         for (int j = 1; j <= x; j++)
  14.             for (int k = 1; k <= m && k < j; k++)
  15.                 table[i][j] += table[i-1][j-k];
  16.  
  17.     return table[n][x]; // forse dobbiamo avere un vettore a 3 dimensioni? table[ ] [ ] [ ] ?
  18. }
  19.  
  20. int main(void){
  21.  
  22. cout << findWays(4, 2, 7) << endl;
  23.  
  24.     // cout << findWays(4, 2, 7) << endl;
  25.     // cout << findWays(4, 3, 6) << endl;
  26.     // cout << findWays(6, 3, 8) << endl;
  27.     // cout << findWays(4, 2, 5) << endl;
  28.     // cout << findWays(4, 3, 5) << endl;
  29.  
  30.     return 0;
  31. }


So how we can return just the ways not counting all the possible permutations?
For example:
findWays(4, 2, 7) have to return just one (because 3+4 or 4+3 is the same)
findWays(4, 3, 6) have to give three ways (1+1+4 and 1+2+3 and 2+2+2)

PM Quote