This article describes the C + + implementation of N-Dice algorithm, share for everyone for reference. The specific methods are as follows:
Title Requirements: Put n dice still on the ground, all points
The implementation code is as follows:
#include <iostream>
using namespace std;
const int g_maxvalue = 6;
const int number = 6;
int array[(number-1) * g_maxvalue + 1];
void probility (int original, int current, int sum, int *array)
{
if (current = = 0)
{
array[sum-original] ++;
return;
}
for (int i = 1; I <= g_maxvalue i++)
{
probility (original, current-1, sum + i, array);
}
}
void Generatevalue (int *array, int size)
{
if (array = NULL | | size <= 0) return
;
for (int i = number; I <= number * g_maxvalue; i++)
{
Array[i-number] = 0;
}
Probility (number, number, 0, array);
for (int i = 0; i < (number-1) * g_maxvalue + 1; i++)
{
cout << array[i] << "";
}
void Main ()
{
Generatevalue (array, (number-1) * g_maxvalue + 1);
}
The loop solution is the same as the code above, but slightly different.
The specific implementation code is as follows:
#include <iostream>
using namespace std;
void printprobability (int number)
{
const int g_maxvalue = 6;
int *array[2];
for (int i = 0; i < 2; i++)
{
Array[i] = new int[g_maxvalue * number];
}
for (int i = 0; i < g_maxvalue * number; i++)
{
Array[0][i] = 0;
Array[1][i] = 0;
}
int flag = 0;
for (int i = 0; i < g_maxvalue i++)
{
array[flag][i] = 1;
}
for (int i = 1; i < number; i++)
{for
(int j = 0; J < i; j +)
array[1-flag][j] = 0;
for (int j = i; J < (i + 1) * G_maxvalue; j + +)
{
array[1-flag][j] = 0;
for (int k = 1; k <= J && k <= G_maxvalue; k++)
array[1-flag][j] + = array[flag][j-k]
;
flag = 1-flag;
}
for (int i = Number-1 I < g_maxvalue * number; i++)
{
cout << array[flag][i] << "";
}
}
void Main ()
{
printprobability (2);
}
I hope this article is helpful to the learning of C + + program algorithm design.