Place n dice on the ground, and the sum of points on all the dice faces up is s. Input n to print all possible values and probabilities of s.
Solution 1: calculate the number of dice points based on recursion.
1 /////////// // calculate the number of dice points based on recursion ///////////////// //////////////////////////////////////// 2 int g_MaxValue = 6; // Number of dice points 3 void Probability (int original, int current, int sum, int * pProbabilities) // original indicates the number of dice and current indicates the number of remaining dice, sum indicates the current and current condition. pProbabilities indicates the number of times the storage is the same and appears. 4 {5 if (current = 0) 6 {7 pProbabilities [sum-original] ++; // The subscript of the array is the current sum minus the minimum sum, the array stores the current and number of occurrences. 8} 9 else10 {11 for (int I = 1; I <= g_MaxValue; I ++) 12 {13 Probability (original, current-1, sum + I, pProbabilities ); 14} 15} 16} 17 18 void Probability (int number, int * pProbabilities) 19 {20 int sum = 0; 21 Probability (number, number, sum, pProbabilities ); 22} 23 24 void PrintProbability (int number) 25 {26 if (number <1) 27 {28 return; 29} 30 int MaxSum = number * g_MaxValue; // The maximum and 31 int size = MaxSum-number + 1; // The possible number of sum 32 int * pProbabilities = new int [size]; 33 for (int I = 0; I <size; I ++) 34 {35 pProbabilities [I] = 0; // initialize to 036} 37 Probability (number, pProbabilities ); 38 double total = pow (double) (g_MaxValue), number); // combination of sums 39 for (int I = 0; I <size; I ++) 40 {41 double ratio = pProbabilities [I]/total; // probability 42 cout <I + number <":" <pProbabilities [I] <": "<ratio <endl; 43} 44 45}