Points of n dice

Source: Internet
Author: User

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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.