[Algorithm 03] sum of n dice

Source: Internet
Author: User

Question: Keep n dice on the ground. The sum of all dice points is S. Input n to print the probability of all possible values of S.

Analysis 1: It is easy to know that if there are n dice, the minimum value of S is n (all is 1), and the maximum value is 6N (all is 6 ).

If there is only one dice, it is very simple. The number of s in the case of 1, 2, 3, 4, 5 and 6 is 1, and the probability is 1/6. Suppose there are n dice, and the sum of appearance is S, we can consider this if the first dice has 6 in the case, take 1, 2, 3, 4, 5, 6; then the sum of N-1-1 dice take s-1, S-2, S-3, S-4, s-5, S-6, we can sum up all of these situations to get the total number of cases. See? What's the problem? By the way, it is still a recursive problem. Based on the above analysis, we can easily write the following recursive formula:

Description of the formula: (1) f (S, N) indicates the total number of cases where n dice exist and the sum is S. (2) Explain the second line of the formula; if there is a dice, the number of points is 8 or 0, And we record it as 0, so that we can make it easier to solve the recursion. For example, we can see that F (8, 2) has a formula) = f () + f () If we specify F) if the value is 0, the calculation is much more convenient.

With the above analysis, we can write the following C ++Code:

 1 # Include <iostream>
2 # Include < String >
3 # Include <cmath>
4 Using Namespace STD;
5
6
7 Int Dicemaxvalue = 6 ;
8 // Calculates the total number of dicenumber dice that appear in all possible conditions for the sum of dicetotalsum.
9 Int Fun ( Int Dicetotalsum, Int Dicenumber)
10 {
11 // The number of dice is less than 1, which is incorrect;
12 If (Dicenumber < 1 )
13 {
14 Cout < " Dicenumber must> = 1 " <Endl;
15 Return 0 ;
16 }
17
18 // Number of dice = 1; if it is between [1, 6] and S, the number of dice is 1;
19 // Otherwise, it is zero. For example, if there is a dice, it is impossible if the number of points is 0 or 8, and 0 is returned.
20 If (Dicenumber = 1 )
21 {
22 If (Dicetotalsum> = dicenumber & dicetotalsum <= dicemaxvalue * dicenumber ))
23 Return 1 ;
24 Else
25 Return 0 ;
26 }
27
28 // N> 1, recursion;
29 If (Dicenumber> 1 )
30 {
31 Int Sum = 0 ;
32 For ( Int I = 1 ; I <= dicemaxvalue; ++ I ){
33 Sum + = fun (dicetotalsum-I, dicenumber- 1 );
34 }
35 Return SUM;
36 }
37 }
38
39 // Given the number of dice, print the probability of various situations
40 Void Printsumprobabilityofdice ( Int Number)
41 {
42 If (Number < 1 )
43 {
44 Return ;
45 }
46
47 Int Maxsum = Number * dicemaxvalue;
48 Float * Pprobability = New Float [Maxsum-Number + 1 ];
49
50 // Array pprobability storage and S
51 For ( Int S = number; S <= maxsum; ++ S)
52 {
53 Pprobability [S-number] = fun (S, number );
54 }
55
56 // Calculate the total number of all cases
57 Int Total = POW (dicemaxvalue, number );
58
59 // Calculate probability and print Probability
60 For ( Int J = 0 ; J <maxsum-Number + 1 ; ++ J)
61 {
62 Pprobability [J]/= total;
63 Cout <j + number < " \ T " <Pprobability [J] <Endl;
64 }
65 Delete [] pprobability;
66 }
67
68
69
70 Int Main ()
71 {
72 Cout < " S " < " \ T " < " Probablility " <Endl;
73 Printsumprobabilityofdice (3 );
74 Return 0 ;
75 }

We calculate the three dice and the running condition is as follows:

In the definition at the beginning, I defined a variable that represents the maximum value of a single dice and assigned a value of 6, which draws on what he Haitao said to ensure the reusability of the Code.

 

Analysis 2: andAlgorithmThe method for solving the Fibonacci series in 2 is similar, the efficiency of recursion is too bad, we can solve it in a forward way, suppose we have an array to represent the number of points in the K-1 dice, so that the second component is expressed as the sum of the total number of cases, then when there are K dice is, the sum of S is the total number of cases, is to represent the K-1 of the array of the S-2 dice s-1, s-3, S-4, S-5, S-6 and (respectively to introduce the K dice values were 1, 2, 3, 4, 5, 6 can be, in fact, and the analysis of the idea of 1 is not too much ). Based on this idea, we can use two arrays to represent the array K-1 and array K, so we have the following code:

# Include <iostream>
# Include < String >
# Include <cmath>
Using Namespace STD;

Int Dicemaxvalue = 6 ;
Void Printprobabilityofdice ( Int Number)
{
Double * Pprobability [ 2 ];
Pprobability [ 0 ] = New Double [Number * dicemaxvalue + 1 ];
Pprobability [ 1 ] = New Double [Number * dicemaxvalue + 1 ];

// Initialize an array
For ( Int I = 0 ; I <Number * dicemaxvalue +1 ; ++ I)
{
Pprobability [ 0 ] [I] = 0 ;
Pprobability [ 1 ] [I] = 0 ;
}

Int Flag = 0 ;
// Assign a value of 1-6 to the subscript of the first Array
For (Int A = 1 ; A <= dicemaxvalue; ++)
{
Pprobability [flag] [a] = 1 ;
}

// The number of dice K cycles from 2 to N. For each K, the value of S is [K, 6 K], and the previous array is calculated for each S.
// The S-1, S-2, S-3, S-4, S-5, S-6; because the minimum value of the previous array is K-1, because S-j> = K-1;
For ( Int K = 2 ; K <= number; ++ K)
{
For ( Int S = K; S <= dicemaxvalue * k; ++ S)
{
Pprobability [ 1 -Flag] [s] = 0 ;
For ( Int J = 1 ; J <= dicemaxvalue & J <= s-K + 1 ; ++ J)
{
Pprobability [1 -Flag] [s] + = pprobability [flag] [S-J];
}
}
Flag = 1 -Flag;
}

// Divide By the total number to calculate and output the probability;
Double Total = POW (( Double ) Dicemaxvalue, number );
For ( Int Ss = number; SS <= Number * dicemaxvalue; ++ SS)
{
Pprobability [flag] [ss]/= total;
Cout <SS < " \ T " <Pprobability [flag] [ss] <Endl;
}


Delete [] pprobability [ 0 ];
Delete [] pprobability [ 1 ];
}

Int Main ()
{
Cout < " S " < " \ T " < " Probability " <Endl;
Printprobabilityofdice ( 4 );
Return 0 ;
}

The running result is as follows:

 

Finally, we can see that, as mentioned in [algorithm 02], although the time complexity of this algorithm has improved a lot, two arrays are created dynamically, in addition, the length of each array is not analyzed. The length of 1 is equal to N, so it is still the idea of changing the space for time. Now, this algorithm is here. I wish you a pleasant stay!

 

References:

[1] He Haitao blog: http://zhedahht.blog.163.com/blog/static/254111742009101524946359/

[2] Wikipedia: http://en.wikipedia.org/wiki/Dice#Probability

[3] http://www.helium.com/items/1538174-how-to-calculate-probability-using-multiple-dice

 

 

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.

 

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.