[Post] points of n dice

Source: Internet
Author: User

From: http://zhedahht.blog.163.com/blog/static/254111742009101524946359/

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.

Analysis: Anyone who has played mahjong knows that dice has six faces, each of which has one point, and the corresponding number is a number between 1 and 6. Therefore, the minimum and number of points of n dice are n and the maximum is 6N. Therefore, an intuitive idea is to define an array with a length of 6 N-N, and save the number of points in S to the nth element of the array. In addition, we also know the number of all points in the n dice is 6 ^ n. Once we calculate the number of occurrences of each point, we only need to divide the number of occurrences of each point by N ^ 6 to obtain the corresponding probability.

The key to this idea is to count the number of occurrences of each point. The number of points and number of n dice are required. We can divide n dice into two heaps: The first Heap has only one and the other has n-1. The individual one may have the number of points from 1 to 6. We need to calculate each point from 1 to 6 and the remaining n-1 dice to calculate the points and. Next we divide n-1-1 dice into two heaps, the first Heap has only one, and the second Heap has a N-2. We add the points of the last round of single dice to the points of this round of separate dice, and then calculate the points and the remaining N-2 dice. From the analysis, we can easily find that this is a recursive idea. The condition for Recursive termination is that there is only one dice left.

Based on this idea, we can write the following code:

int g_maxValue = 6;

void PrintSumProbabilityOfDices_1(int number)
{
if(number < 1) return;

int maxSum = number * g_maxValue;
int* pProbabilities = new int[maxSum - number + 1];
for(int i = number; i <= maxSum; ++i)
pProbabilities[i - number] = 0;

SumProbabilityOfDices(number, pProbabilities);

int total = pow((float)g_maxValue, number);
for(int i = number; i <= maxSum; ++i) {
float ratio = (float)pProbabilities[i - number] / total;
printf("%d: %f\n", i, ratio);
}

delete[] pProbabilities;
}

void SumProbabilityOfDices(int number, int* pProbabilities)
{
for(int i = 1; i <= g_maxValue; ++i)
SumProbabilityOfDices(number, number, i, 0, pProbabilities);
}

void SumProbabilityOfDices(int original, int current, int value, int tempSum, int* pProbabilities)
{
if(current == 1) {
int sum = value + tempSum;
pProbabilities[sum - original]++;
}
else {
for(int i = 1; i <= g_maxValue; ++i) {
int sum = value + tempSum;
SumProbabilityOfDices(original, current - 1, i, sum, pProbabilities);
}
}
}

The above algorithms provide excellent performance when the number is small. However, because the algorithm is based on recursion, many computations are repeated, resulting in unacceptable performance when the number increases. For details about the performance of recursive algorithms, see the 16th questions in this blog series.

We can consider another way to solve this problem. We can consider using two arrays to store the number of times each number of dice appears. In a loop, the nth number in the first array represents the number of times the dice and N appear. In the next loop, we will add a new dice. Then the number of times that n dice appear at this time should be equal to the number of dice in the previous cycle and the sum of N-1, N-2, n-3, n-4, N-5 and n-6. So we set the N number of another array to the sum of n-1, N-2, n-3, n-4, and N-5 corresponding to the previous array. Based on this idea, we can write the following code:

void PrintSumProbabilityOfDices_2(int number)
{
double* pProbabilities[2];
pProbabilities[0] = new double[g_maxValue * number + 1];
pProbabilities[1] = new double[g_maxValue * number + 1];
for(int i = 0; i < g_maxValue * number + 1; ++i) {
pProbabilities[0][i] = 0;
pProbabilities[1][i] = 0;
}

int flag = 0;
for (int i = 1; i <= g_maxValue; ++i)
pProbabilities[flag][i] = 1;

for (int k = 2; k <= number; ++k) {
for (int i = k; i <= g_maxValue * k; ++i) {
pProbabilities[1 - flag][i] = 0;
for(int j = 1; j <= i && j <= g_maxValue; ++j)
pProbabilities[1 - flag][i] += pProbabilities[flag][i - j];
}
flag = 1 - flag;
}

double total = pow((double)g_maxValue, number);
for(int i = number; i <= g_maxValue * number; ++i) {
double ratio = pProbabilities[flag][i] / total;
printf("%d: %f\n", i, ratio);
}

delete[] pProbabilities[0];
delete[] pProbabilities[1];
}

It is worth mentioning that the above Code does not hard encode the maximum number of points of a dice in the function (hard CoDe)It is represented by a variable g_maxvalue. In this case, if a manufacturer produces a dice with a maximum of 4 or 8 points, we only need to modify a place in the Code to facilitate expansion. If we can consider program scalability during the interview, we will be able to leave a good impression on the interviewer.

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.