43.N Dice points

Source: Internet
Author: User

Solution One: Based on recursive calculation of dice points


void printprobability (int number)
{
if (number < 1)
Return
int maxsum = Number*g_maxvalue;
int* printprobability = 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);
}
}
}

Solution Two: Counting dice points based on cycle


void printprobability (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 i = k; I <= g_maxvalue*k; ++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];
}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

43.N Dice points

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.