[Programming question] points of n dice

Source: Internet
Author: User

67. Two games (Computing ).
2. n dice points.
Place n dice on the ground, and the sum of all dice toward the point above is S. Input N,
Print the probability that all possible values of S appear.

 

Idea: recursively convert the possible condition variables of each dice, and record the total number of occurrences of various and S as probability.

/* 67. Two playmates (Computing ). 2. n dice points. Place n dice on the ground, and the sum of all dice toward the point above is S. Input n to print the probability of all possible values of S. */# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <math. h> void getallarr (int n, int * record) // recursively obtain each possible occurrence and Occurrence Count {static int sum = 0; static int num = N; if (0 = N) {record [Sum-num] ++; return ;}for (INT I = 1; I <= 6; I ++) {sum + = I; getallarr (n-1, record); sum-= I ;}} void printall (INT N) {If (0 = N) {printf ("input error");} int * record = (int *) malloc (5 * n + 1) * sizeof (INT); memset (record, 0, (5 * n + 1) * sizeof (INT); getallarr (n, record); For (INT I = N; I <= 6 * n; I ++) {printf ("% d: % d/% d \ n", I, record [I-n], INT (POW (6.0, n )));}
Free (record);} int main () {printall (8); Return 0 ;}

 

Looking at the answers on the Internet, we found that we can use dynamic planning.

Http://blog.csdn.net/whuqin/article/details/6639187

1. The current variables include: number of dice, points, and. When there are K dice and the sum of points is N, the number of occurrences is recordedF (k, n). What is the relationship with the K-1 dice stage?

2. When I have a K-1 dice and then add a dice, the dice points can only be 1, 2, 3, 4, 5 or 6. The K dice get the sum of points and N:

(K-1, n-1): Number 1 for the K dice

(K-1, N-2): K dice shot points 2

(K-1, n-3): K dice shot points 3

....

(K-1, n-6): K dice shot 6 points

On the basis of the K-1 dice, then add a dice appear points and N results only these 6 cases!

Therefore:F (k, n) = f (K-1, n-1) + f (K-1, N-2) + f (K-1, n-3) + f (K-1, n-4) + f (K-1, n-5 (K-1, n-6) + f)

3. There is 1 dice, F () = f () = 1.

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.