Points of n dice. Place n dice on the ground, and the sum of all dice toward the point above is S. Input n to print the probability of all possible values of S.
Thought: F (N, S) = f (n-1, S-6) + f (n-1, S-5) + f (n-1, S-4) + f (n-1, S-3) + f (n-1, S-2) + f (n-1, s-1 );
# Include <stdio. h> # include <stdlib. h> # include <string. h> int power (INT base, unsigned int N) {If (n = 0) return 1; if (n = 1) return base; int half = power (base, n> 1); Return (N & 1) = 1? Base: 1) * half * Half;} // recursive int F (int n, int s) {If (S <n | S> 6 * n) return 0; if (n = 1) return 1; // If (S = n | S = 6 * n) return 1; elsereturn F (n-1, S-6) + f (n-1, S-5) + f (n-1, S-4) + f (n-1, S-3) + f (n-1, S-2) + f (n-1, s-1 );} void listdiceprobability (int n) {int I = 0; unsigned int ntotal = power (6, n); for (I = N; I <= 6 * n; I ++) {printf ("P (S = % d) = % d/% d \ n", I, F (n, I), ntotal );}} // non-recursive void pr_probabilityofdices (unsigned int N) {const unsigned Int max = 12; int I = 0, size = 0, S, N; If (n> MAX) {printf ("too big! \ N "); return;} // new arrayint ** F = malloc (sizeof (int *) * (n + 1); for (I = 1; I <n + 1; I ++) {size = sizeof (INT) * (6 * I + 1); F [I] = (int *) malloc (size ); memset (F [I], 0, size);} // initfor (I = 1; I <= 6; I ++) {f [1] [I] = 1 ;}// updatefor (n = 2; n <n + 1; n ++) {for (S = N; S <= (N * 6); s ++) {f [N] [s] = 0; for (I = 1; I <= 6; I ++) if (s-I> = n-1 & S-I <= 6*(n-1 )) f [N] [s] + = f [n-1] [s-I] ;}} unsigned int ntotal = power (6, n); for (I = N; I <6 * n + 1; I ++) {printf ("P (S = % d) = % d/% d \ n", I, F [N] [I], ntotal) ;}// free arrayfor (I = 1; I <n + 1; I ++) Free (F [I]); free (f);} // same as void printsumprobabilityofdices (unsigned int N) {const unsigned int max = 12; // Max number of dicesif (n> MAX) {printf ("overflow! \ N "); Return ;}; unsigned int A [Max + 1] [6 * MAX + 1]; unsigned int N, S, I; memset (A, 0, sizeof (a); For (S = 1; S <= 6; s ++) A [1] [s] = 1; for (n = 2; n <= N; n ++) for (S = N; S <= 6 * n; s ++) {a [n] [s] = 0; for (I = 1; I <= 6 & I <s; I ++) a [n] [s] + = A [n-1] [s-I];} unsigned int ntotal = power (6, n); for (I = N; I <= 6 * n; I ++) {printf ("P (S = % d) = % d/% d \ n ", I, a [n] [I], ntotal) ;}} int main (void) {puts ("*************"); listdiceprobability (3); puts ("***********"); pr_probabilityofdices (3); Return 0 ;}