Using C language to solve the problem of the number of cards and n dice _c language

Source: Internet
Author: User

the Shun Zi
    question description: Randomly draw 5 cards from poker to determine if the 5 cards are consecutive. 2-10 is the number itself, A is 1,j for 11,q for 12,k 13, and the size king can be seen as any number.
         Ideas: You can order the 5 cards, and then count the number of 0 and not 0 number of intervals between the number, if there is a repeat of not 0 digits, then not the CIS. If the number of intervals is less than or equal to 0, then the Junko. There was no better way to think about it for a while.
         Reference Code:

/function function: Randomly draw 5 cards from poker to determine if it is not a CIS//function parameter: Pcards is a card, Nlen is the number of cards//return value: Whether or not shun Zi bool Iscontinu 
 
 ous (int *pcards, int nlen) {if (pcards = = NULL | | Nlen <= 0) return false; Sort (pcards, pcards + nlen); 
 Call the sorting algorithm of the standard library int i; int zerocount = 0; King size 0 for int capcount = 0; 
  Number of intervals//statistics 0 for (i = 0; i < Nlen; i++) {if (pcards[i) = 0) zerocount++; 
 else break;  
 }//statistic interval int precard = pcards[i]; 
  for (i = i + 1; i < Nlen i++) {int curcard = pcards[i]; 
  if (Precard = = Curcard)//Compare return False to the previous card; 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 a dice of points
Problem Description: Throw n dice on the ground, and the sum of points on the upper side of the dice is S. Enter N to print out the probability that all of the possible values of s are present.
Idea: This is a problem of applying dynamic programming thought, and dynamic programming is the hardest to find the best substructure. And take a method called a memo to avoid repetitive computations. Because the memo method establishes a memo for each of the solved child problems, it is necessary to refer to them to avoid duplicate solutions of the same child problems.
The optimal substructure of the subject is: F (k, n) is the number of K-Dice and N, and K is the number of dice, n means K dice and

     /= 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.
From the formula above, we can see that the K-dice and the number of n are only related to the k-1 of the dice. This allows you to use the Memo method, save the solution of the resolved child problem with a table, and then fill in the form from the bottom up. You only need to save one row, considering that the current layer's calculations are only related to the next level.
Reference code:

const int face_num = 6; Dice Face//function function: N dice//function parameters: number is dice//return value: no void printsumprobabilityofdices (int numbers) {if 
 
 <= 0) return; int *psum = new Int[number * face_num + 1]; And the kind of double total = POW (6.0, number); 
 <cmath> int size = number * FACE_NUM; 
 
 int i,j,k; 
 Initialize 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++)//dice numbers from 2 to n {for (j = i * face_num; J >= i; j--)//I dice and the range is [I, I*face_num] { 
   PSUM[J] = 0;  for (k = 1; k <= 6 && J >= K; k++)//actually unfold 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]; 
 }//Impossible situation, that is, I dice and cannot be less than I for (j = i-1;j >= 0; j--) psum[j] = 0; //print result for (i = 0; I <= size; i++) cout<< "sum = <<i<<", p = "<<psum[i"/TOTAL&LT;&L 
T;endl; 
 }

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.