-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.c
48 lines (43 loc) · 1.24 KB
/
dice.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
while or recursive
while use two array to rember the n-1 n-2 n-3 n-4 n-5 n-6
*/
int g_maxvalue = 6;//the dice max number
void s_get_probable(int original, int current, int sum, int* probable){
if(1 == current){
probable[sum - original]++;
}else{
int i = 1;
for(;i<=g_maxvalue;++i)
s_get_probable(original,current-1,sum+i,probable);
}
}
void get_probable(int count, int* probable){
int i = 1;
for(;i<=g_maxvalue;++i){
s_get_probable(count,count,i,probable);
}
}
void print_probable(int count){
if(count < 1)
return ;
int max = count * g_maxvalue;
int* probable = (int*)malloc( (max - count + 1)*sizeof(int) );
if(probable == NULL)
return;
int i = count;
double total;
int race;
for(;i<=max;++i){
probable[i-count] = 0;
}
get_probable(count,probable);
total = pow((double)g_maxvalue,count);
for(i=count;i<=max;i++){
race = probable[i-count] / total;
printf("%d: %e\n",i,race);
}
}