Number of points for training n dice during the interview

Source: Internet
Author: User

Question:NA dice is thrown to the ground, and the sum of all the dice toward the point aboveS. InputN, Print outSThe probability that all possible values.

To use dynamic planning, you still need to consider the idea ....

There is a simple method to solve this problem, that is, recursion.

Thought of the first dice one in 1-6, followed by n-1 sieve. Just calculate a sum.

# Include "stdio. H "int gmax = 6; void propcount (INT origin, int number, int sum, int * prop) {int I = 1; if (number = 1) {prop [Sum-origin] ++;} else {for (I = 1; I <= gmax; I ++) {propcount (origin, number-1, sum + I, Prop) ;}} void getprop (INT num) {int sum = 0; int I = 0; int * prop = (int *) malloc (sizeof (INT) * (Num * gmax-num + 1); memset (prop, 0, sizeof (INT) * (Num * gmax-num + 1 )); if (Num <= 0) return; for (I = 1; I <= gmax; I ++) propcount (Num, num, sum + I, Prop ); for (I = 0; I <num * gmax-num + 1; I ++) printf ("% d", prop [I]); free (PROP );} int main () {int num = 3; getprop (Num); Return 0 ;}

Through reading books, we found that the idea in the book is to establish a relationship by increasing the number of dice.

Two arrays need to be set

The first array n indicates the sum of the number of times the sieve is N.

The second array added a sieve n representing n-1, N-2, n-3, n-4, N-5, n-6, times.

The implementation of reading is very skillful. It took me some time to understand the principle.

 # include "stdio. H "int gmax = 6; # define Max 1024 void getprop (INT num) {int prop [2] [Max]; int I = 0, j = 0; int flag = 0; int K; for (I = 0; I 
  

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.