The C language is used to solve the problem of playing CARDS and n dice

  • 2020-05-09 19:01:02
  • OfStack

A favorite of playing CARDS
Problem description: draw 5 CARDS at random from playing CARDS to judge whether they are a straight line or not. 2-10 is the number itself, A is 1, J is 11, Q is 12, K is 13, and xiao wang can view it as any number.
                idea: you can arrange these 5 CARDS in a sequence, and then count the number of zeros and the number of intervals between non-zero digits. If the number of intervals is less than or equal to 0, then it's a shunzi. No better way has been thought of yet.
                reference code:


// The functionality   :   Draw randomly from playing CARDS 5 A card, judge 1 A shunza  
// Function parameters   :  pCards For the brand, nLen Is the number of CARDS  
// The return value   :   Whether shunza  
bool IsContinuous(int *pCards, int nLen) 
{ 
 if(pCards == NULL || nLen <= 0) 
  return false; 
 
 sort(pCards, pCards + nLen); // Calls the sorting algorithm of the standard library  
 
 int i; 
 int zeroCount = 0; // Big wang 0 said  
 int capCount = 0; // Interval number  
 // statistical 0 The number of  
 for(i = 0; i < nLen; i++) 
 { 
  if(pCards[i] == 0) 
   zeroCount++; 
  else 
   break; 
 } 
 // Number of statistical intervals  
 int preCard = pCards[i];  
 for(i = i + 1; i < nLen; i++) 
 { 
  int curCard = pCards[i]; 
  if(preCard == curCard) // With the former 1 CARDS are  
   return false; 
  else 
   capCount += curCard - preCard - 1; // Cumulative interval number  
  preCard = curCard; 
 } 
 return (zeroCount >= capCount)? true: false; // As long as the number of Kings is greater than the number of intervals  
} 

n dice
Problem description: throw n dice on the ground. The sum of the dice with one side up is S. Enter n and print out the probability of the occurrence of all possible values of S.
                idea: this is the first application of the idea of dynamic programming, and the most difficult part of dynamic programming is to find the optimal substructure. And adopt a method called memorandum to avoid double counting. Because the memo method establishes a memo for each subproblem solved, it is available when needed, and avoids the repeated solution of the same subproblem.
The optimal substructure of             is: F(k, n) means the number of dice and the number of species of n; k means the number of dice and n means the number of dice and k k


     /  = F(k-1, n-6) + F(k-1, n-5) + F(k-1, n-4) + F(k-1, n-3) + F(k-1, n-2) + F(k-1, n-1)   for  k > 0, k <= n <= 6*k
 F(k, n) =  
     \  = 0     for  n < k or n > 6*k

When k=1, F(1,1)=F(1,2)=F(1,3)=F(1,4)=F(1,5)=F(1,6)=1.
As can be seen from the above formula,                 can only be related to the sum of k-1 dice. This allows you to use the memo method, which is to use a table to hold the solutions to the solved subproblems, and then fill in the table from bottom to top. Considering that the current layer's calculations only relate to the next layer, you only need to save 1 row.
                reference code:


const int FACE_NUM = 6; // The number of faces on a die  
 


// The functionality   :  n The number of dice  
// Function parameters   :  number For the number of dice  
// The return value   :   There is no  
void PrintSumProbabilityOfDices(int number) 
{ 
 if(number <= 0) 
  return; 
 
 int *pSum = new int[number * FACE_NUM + 1]; // And the type of  
 double total = pow(6.0, number); //<cmath> 
 int size = number * FACE_NUM; 
 int i,j,k; 
 
 // Initialize the  
 pSum[0] = 0; 
 for(i = 1; i <= FACE_NUM; i++) 
  pSum[i] = 1; 
 for(; i <= size; i++) 
  pSum[i] = 0; 
 
 for(i = 2; i <= number; i++) // Number of dice 2 to n 
 { 
  for(j = i * FACE_NUM; j >= i; j--) // The first i The sum of the dice is zero  [i, i*FACE_NUM] 
  { 
   pSum[j] = 0; 
   for(k = 1; k <= 6 && j >= k; k++) // And the way it unfolds is  F(i, j) = F(i-1, j-6) + F(i-1, j-5) + F(i-1, j-4) + F(i-1, j-3) + F(i-1, j-2) + F(i-1, j-1) 
   { 
    pSum[j] += pSum[j-k]; 
   } 
  } 
  // The impossible case, namely i The sum of the dice cannot be less than i 
  for(j = i - 1;j >= 0; j--) 
   pSum[j] = 0; 
 } 
 
 // Print the result  
 for(i = 0; i <= size; i++) 
  cout<<"sum = "<<i<<", p = "<<pSum[i] / total<<endl; 
} 


Related articles: