n Points of a dice

Source: Internet
Author: User

Title: Throw n Dice on the ground, all dice on the upper side of the sum of points for S, enter N, print out the probability that all possible values of s appear. Solution One: Based on recursive calculation of dice points, time efficiency is not high. Now we consider how to count the occurrences of each point. To find out the number of n dice, you can first divide n dice into two piles: the first pile is only one, the other has a n-1, The single one has the potential to show points from 1 to 6. We need to calculate the number of points from 1 to 6 and the rest of the n-1 dice to calculate the number of pips and. Then the rest of the n-1 dice are divided into two piles, the first pile only one, the second pile of n-2. We add the points of the last round of the individual dice and the points of the individual sieve, and then count the points with the rest of the n-2 dice. The condition of the end of recursion is that there is only one die left. Based on this idea, the code is referenced as follows:
1 intG_maxvalue=6;2 voidPrintprobability (intNumber )3 {4   if(number<1)5   return;6   intmaxsum=num*G_maxvalue;7   int* pprobabilities=New int[maxsum-number+1];8    for(inti=number;i<=maxsum;++i)9pprobabilities[i-number]=0;Ten probability (number,pprobabilities); One   intTotal=pow ((Double) g_maxvalue,number); A    for(inti=number;i<=maxsum;++i) -   { -    DoubleRatio= (Double) pprobabilities[i-number]/Total ; theprintf"%d:%e\n", i,ratio); -   } -   Delete[]probabilities; -  } +  - voidProbability (intNumberint*pprobabilities) + { A   for(intI=1; i<=g_maxvalue;++i) at probability (number,number,i,pprobabilities); -  } -  - voidProbability (intOriginalintCurrentintSumint*pprobabilities) - { -   if(current==1) in{pprobabilities[sum-original]++; } -  Else to    { +     for(intI=1; i<=g_maxvalue;++i) -{Probabilitiey (original,current-1, i+sum,pprobabilities);} the    } *  }

These ideas are simple and easy to implement. But because it is based on recursive implementations, it has many computations that are repetitive, resulting in slow performance when number becomes large.

Solution Two: Based on the cycle of dice points, time performance can be replaced by a way of thinking to solve the problem. We can consider using two arrays to store the number of occurrences of each of the dice. In a loop, the nth digit in the first array represents the number of times the dice and N appear. In the next loop, we add a new dice, and the number of times that the dice for n should be equal to the number of dice in the last cycle and the sum of the Times for N-1,n-2,n-3,n-4,n-5 and n-6. So we set the nth number of the other array to the sum of the n-1,n-2,n-3,n-4,n-5 and n-6 corresponding to the previous array. Based on this idea, the reference code is as follows:
1 intG_maxvalue=6;2 voidPrintprobability (intNumber )3 {4  if(number<1)return;5  int*pprobabilities[2];6pprobabilities[0]=New int[g_maxvalue*number+1];7pprobabilities[1]=New int[g_maxvalue*number+1];8   for(intI=0; i<g_maxvalue*number+1;++i)9  {Tenpprobabilities[0][i]=0; Onepprobabilities[1][i]=0; A  } -  -  intflag=0; the   for(intI=1; i<=g_maxvalue;++i) -pprobabilities[flag][i]=1; -  -   for(intk=2; k<=number;++k) +  { -     for(intI=0; i<k;++i) +pprobabilities[1-flag][i]=0;  A     at      for(inti=k;i<=g_maxvalue*k;++i) -   { -pprobabilities[1-flag][i]=0; -      for(intj=1; j<=i&&j<=g_maxvalue;++j) -pprobabilities[1-flag][i] +=pprobabilities[flag][i-j]; -   } inflag=1-Flag; -  } to  + DoubleTotal=pow ((Double) g_maxvalue,number); -    for(inti=number;i<=g_maxvalue*number;++i) the   { *    DoubleRatio= (Double) pprobabilities[flag][i]/Total ; $printf"%d:%e\n", i,ratio);Panax Notoginseng   } - Delete[]pprobabilities[0]; the Delete[]pprobabilities[1]; +}
In the above code, we defined two arrays of pprobabilities[0] and pprobabilities[1] to store the sum of the points of the dice. In a round loop, the nth item of an array equals the sum of the n-1,n-2,n-3,n-4,n-5,n-6 of the other array. In the next round of loops, we swap the two arrays (implemented by changing the flag of the variable) and repeat the calculation process. It is worth noting that the code above does not hardcode the maximum number of points in a dice to 6 in a function, but instead uses a variable g_maxvalue to represent it. The advantage of this is that if a factory produces other points of the dice, we only need to modify a place in the code, the extension is very convenient.

n Points of a dice

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.