Question: Put n dice on the ground, and the sum of all the dice toward the point above is S. Input n to print the probability of all possible values of S.
UsePn, STo indicateNDice, sum of top numbersSThe probability of occurrence, then:
Pn, S = An, S/6 ^ n
WhereAn, SIsNSum of top number of diceSNumber of States,6 ^ nIs the total number of States because each dice has6Status,NThe number of States composed of dice is6 ^ n.
The following is an example.AI, JTHE RECURSIVE FORMULAI (1 = <I <= N)Sum of top number of diceJ (I = <j <= 6 * I)Number of statuses:
AI, j = ai-1, J-1//IDice top is1OtherI-1Sum of top number of diceJ-1Number of statuses
+ Ai-1, J-2//IDice top is2OtherI-1Sum of top number of diceJ-2Number of statuses
+I-1, J-3//IDice top is3OtherI-1Sum of top number of diceJ-3Number of statuses
+I-1, j-4//IDice top is4OtherI-1Sum of top number of diceJ-4Number of statuses
+I-1, J-5//IDice top is5OtherI-1Sum of top number of diceJ-5Number of statuses
+I-1, j-6//IDice top is6OtherI-1Sum of top number of diceJ-6Number of statuses
In the recursive formulaI> 1.
For any1 = <I <= N,J <= 0OrJ <IOrJ> 6 * I,AI, j = 0.
The following is the implementation code of algorithm C ++.
void PrintSumProbabilityOfDices(unsigned int n){const unsigned int MAX=12; //max number of dicesif(n>MAX){printf("Overflow!\n");return;};unsigned int a[MAX+1][6*MAX+1];unsigned int i,j,k;memset(a,0,sizeof(a));for(j=1;j<=6;j++)a[1][j]=1;for(i=2;i<=n;i++)for(j=i;j<=6*i;j++){a[i][j]=0;for(k=1;k<=6&&k<=j;k++)a[i][j]+=a[i-1][j-k];}unsigned int nTotal=pow(6,n);for(i=n;i<=6*n;i++)printf("Sum=%d,Probability=%.15lf\n",i,a[n][i]*1.0/nTotal);}