// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class Program { private static void Find(int[] n, int m, long scale, long[] good, ref long total) { if(m > 0) { for(int i = 0; i < n.Length; i++) { if(n[i] > 0) { int sav = n[i]; n[i] = 0; Find(n, m - 1, scale * sav, good, ref total); n[i] = sav; } } } else { for(int ix = 0; ix < n.Length; ix++) { if(n[ix] == 0) { good[ix] += scale; } } total += scale; } } public static void Calc(int[] n, int m) { long[] good = new long[n.Length]; long total = 0; Find(n, m, 1, good, ref total); double[] p = new double[n.Length]; for(int i = 0; i < n.Length; i++) { p[i] = good[i] / (double)total; } double sum = 0; for(int i = 0; i < n.Length; i++) { Console.WriteLine(n[i] + " => " + p[i]); sum += p[i]; } Console.WriteLine(m + "=" + sum); } public static void Main(string[] args) { Calc(new int[] { 10, 1 }, 2); Calc(new int[] { 10, 1, 1 }, 2); Calc(new int[] { 50, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 10); Calc(new int[] { 50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 10); Calc(new int[] { 50, 33, 10, 10, 1, 1, 1, 1, 1, 1, 1 }, 10); } } }