The number of points of n dice of offer (43)

Source: Internet
Author: User

Question: Put n dice on the ground, all dice toward the sum of points on the top of S, input N, and print the probability of all possible values of S.

Int g_maxvalue = 6; // ================================== method 1 ========================== === void probability (INT number, int * pprobabilities); void probability (INT original, int current, int sum, int * pprobabilities); void printprobability_solution1 (INT number) {If (number <1) return; int maxsum = Number * g_maxvalue; int * pprobabilities = new int [maxsum-Number + 1]; for (INT I = number; I <= maxsum; ++ I) pprobabilities [I-number] = 0; probability (number, pprobabilities); int Total = POW (double) g_maxvalue, number); For (INT I = number; I <= maxsum; ++ I) {double ratio = (double) pprobabilities [I-number]/total; printf ("% d: % E \ n", I, ratio);} Delete [] pprobabilities;} void probability (INT number, int * pprobabilities) {for (INT I = 1; I <= g_maxvalue; ++ I) probability (number, number, I, pprobabilities);} void probability (INT original, int current, int sum, int * pprobabilities) {If (current = 1) {pprobabilities [Sum-original] ++;} else {for (INT I = 1; I <= g_maxvalue; ++ I) {probability (original, current-1, I + sum, pprobabilities );}}} // =================================== method 2 ======================== === void printprobability_solution2 (INT number) {If (number <1) return; int * pprobabilities [2]; pprobabilities [0] = new int [g_maxvalue * Number + 1]; pprobabilities [1] = new int [g_maxvalue * Number + 1]; for (INT I = 0; I <g_maxvalue * Number + 1; ++ I) {pprobabilities [0] [I] = 0; pprobabilities [1] [I] = 0;} int flag = 0; For (INT I = 1; I <= g_maxvalue; ++ I) pprobabilities [flag] [I] = 1; for (int K = 2; k <= number; ++ K) {for (INT I = 0; I <K; ++ I) pprobabilities [1-Flag] [I] = 0; For (INT I = K; I <= g_maxvalue * k; ++ I) {pprobabilities [1-Flag] [I] = 0; For (Int J = 1; j <= I & J <= g_maxvalue; ++ J) pprobabilities [1-Flag] [I] + = pprobabilities [flag] [I-j];} flag = 1-flag;} double Total = POW (double) g_maxvalue, number); For (INT I = number; I <= g_maxvalue * number; ++ I) {double ratio = (double) pprobabilities [flag] [I]/total; printf ("% d: % E \ n", I, ratio);} Delete [] pprobabilities [0]; Delete [] pprobabilities [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.