Points for the "offer of Swords" (N Dice) (Java)

Source: Internet
Author: User
Tags pow

Introduction: The original intention of writing this article is just to make a note, because the problem code is a bit large, a bit abstract, and the book does not have detailed comments. In order to deepen the impression and facilitate the next review, make a record.

The original question: Throw n dice on the ground, all the dice on the upper side of the point after the S. Enter N to print out the probability that all possible values of s appear. (6 faces per dice, points from 1 to 6)

Solution One: Based on recursion, time efficiency is not high

Recursive thought is generally divide and conquer, the N dice into the first and the rest of the n-1. Calculate the number of occurrences of each point in the first dice, and then calculate the sum of the points that appear in the remaining n-1 dice. Find n-1 dice points of the method and the same as before, that is, n-1 dice divided into two piles------the first and the remaining n-2. N Dice, 6 sides of each dice, a total of 6n combinations. There must be a repetition of these 6n combinations, we know that the range is n~6n, and for each case we can record it with a caching mechanism, and whenever it happens we make its corresponding unit add 1.

We define an array of length 6n-n+1, and the number of points for s to be saved to the N-s element of the array. Why is it 6n-n+1? Because n dice and the minimum is n, the maximum is 6n, between the two can occur in each case, a total of 6n-n+1. Here is the Java source code:

1     Private Static Final intG_maxvalue = 6;2     //time efficiency is not high based on recursive counting of dice points3      Public Static voidPrintprobability (intNumber ) {4         if(number<1)return;5         intMaxsum = number*G_maxvalue;6         int[] Pprobabilities =New int[Maxsum-number+1];7         //initialized, 0 times before starting statistics8          for(inti=number;i<=maxsum;i++){9Pprobabilities[i-number] = 0;Ten         } One         DoubleTotal =Math.pow (g_maxvalue,number); A         //probability (number,pprobabilities); This function calculates the number of occurrences of n~6n in each case - probability (number,pprobabilities); -          for(inti=number;i<=maxsum;i++){ the             DoubleRatio = pprobabilities[i-number]/Total ; -System.out.println ("I:" +i+ "ratio:" +ratio); -         } -     } +      Public Static voidProbability (intNumberint[] pprobabilities) { -          for(inti=1;i<=g_maxvalue;i++) {//start with the first dice + probability (number,number,i,pprobabilities); A         } at     } -     //a total of original dice, current first dice, current and, through the Always array -      Public Static voidProbability (intOriginalintCurrentintSumint[] pprobabilities) { -         if(current==1){ -pprobabilities[sum-original]++; -}Else{ in              for(inti=1;i<=g_maxvalue;i++){ -Probability (original,current-1,sum+i,pprobabilities); to             } +         } -}

This method of thinking is very concise, but recursive implementation of sub-problem can be solved repeatedly, so when the number is very large, its performance will be slow to be unacceptable.

Solution Two: Based on the cycle, good time performance

Recursion is usually the top-down analytic solution, and the loop-based approach is bottom-up. Loop-based generally requires less space and less time, with better performance, but the general code is more difficult to understand.

The explanation in the book is relatively simple, the code does not have the comment, here I use Java to realize the method on the book, the comment is more detailed.

1  //Dice counting based on loop2      Public Static voidPrintprobability_1 (intNumber ) {3         if(number<1){4             return;5         }6         int[] Pprobabilities =New int[2] [G_maxvalue*number +1];7          for(inti=0;i<g_maxvalue;i++) {//initializing an array8Pprobabilities[0][i] = 0;9Pprobabilities[1][i] = 0;Ten         } One         intFlag = 0; A          for(inti=1;i<=g_maxvalue;i++) {//There are 6 possibilities when throwing the dice for the first time, each of which may occur once -Pprobabilities[flag][i] = 1; -         } the         //Roll the dice from the second start, assuming that the nth number in the first array represents the number of times the dice and n appear, -         //in the next loop, we add a new dice, and the number of dice for n should be equal to the dice points in the last cycle and n-1,n-2,n-3,n-4,n-5, -         //n-6, so we set the nth number of the other array to the sum of the n-1,n-2,n-3,n-4,n-5,n-6 of the previous array . -          for(intK =2;k<=number;k++){ +              for(inti=0;i<k;i++) {//K-Roll dice, and the smallest k, less than the case of K is not possible! So another unlikely occurrence is set to 0!  -Pprobabilities[1-flag][i] = 0; +             } A              for(inti=k;i<=g_maxvalue*k;i++) {//K-Roll dice, and Min. k, max. G_maxvalue*k atPprobabilities[1-flag][i] = 0;//Initialize, because this array is to be reused, the last value should be cleared 0 -                  for(intj=1;j<=i&&j<=g_maxvalue;j++){ -Pprobabilities[1-flag][i] + = pprobabilities[flag][i-j]; -                 } -             } -Flag = 1-Flag; in         } -         DoubleTotal =Math.pow (g_maxvalue, number); to          for(inti=number;i<=g_maxvalue*number;i++){ +             DoubleRatio = pprobabilities[flag][i]/Total ; -System.out.println ("Sum:" +i+ "ratio:" +ratio); the         } *}

Operation Result:

Sum:6 ratio:2.143347050754458e-5
Sum:7 ratio:1.286008230452675e-4
Sum:8 ratio:4.501028806584362e-4
Sum:9 ratio:0.0012002743484224967
Sum:10 ratio:0.002700617283950617
Sum:11 ratio:0.005401234567901234
Sum:12 ratio:0.00977366255144033
Sum:13 ratio:0.016203703703703703
Sum:14 ratio:0.02488425925925926
Sum:15 ratio:0.03570816186556927
Sum:16 ratio:0.048161008230452676
Sum:17 ratio:0.061213991769547324
Sum:18 ratio:0.07353823731138547
Sum:19 ratio:0.08371913580246913
Sum:20 ratio:0.09047067901234568
Sum:21 ratio:0.09284979423868313
Sum:22 ratio:0.09047067901234568
Sum:23 ratio:0.08371913580246913
Sum:24 ratio:0.07353823731138547
Sum:25 ratio:0.061213991769547324
Sum:26 ratio:0.048161008230452676
Sum:27 ratio:0.03570816186556927
Sum:28 ratio:0.02488425925925926
Sum:29 ratio:0.016203703703703703
Sum:30 ratio:0.00977366255144033
Sum:31 ratio:0.005401234567901234
Sum:32 ratio:0.002700617283950617
Sum:33 ratio:0.0012002743484224967
Sum:34 ratio:4.501028806584362e-4
Sum:35 ratio:1.286008230452675e-4
Sum:36 ratio:2.143347050754458e-5

Points for the "offer of Swords" (N Dice) (Java)

Related Article

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.