%52 正好有一双 书本后答案 0.4268 计算机模拟结果 0.4268
2*(factorial(8) * 2^6/(2)) * nchoosek(10,1)*nchoosek(9,6)/(20*19*18*17*16*15*14*13) %书上的差一倍
nchoosek(10,1)*18*16*14*12*10*8/(nchoosek(20,8) * factorial(6)) %分子分母按组合来取
%先取1双的组合数有10,然后再取6双的排列数,将排列数转乘组合数 是除以6!
56*nchoosek(10,1)*18*16*14*12*10*8/(20*19*18*17*16*15*14*13) %分子分母按排列数来取
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Diagnostics; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //Stopwatch sw = new Stopwatch(); //sw.Start(); T4(); //sw.Stop(); //Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); } private static void T1() { var list = new List<List<int>>(); var count = 0; StringBuilder sb = new StringBuilder(); for (int f1 = 1; f1 <= 6; f1++) { for (int f2 = 1; f2 <= 6; f2++) { for (int f3 = 1; f3 <= 6; f3++) { for (int f4 = 1; f4 <= 6; f4++) { for (int f5 = 1; f5 <= 6; f5++) { var dic = new int[] { 0, 0, 0, 0, 0, 0, 0 }; dic[f1]++; dic[f2]++; dic[f3]++; dic[f4]++; dic[f5]++; var pcount = 0; var tcount = 0; foreach (int it in dic) { if (it == 2) { pcount++; } if (it > 2) { tcount++; } } if (pcount == 2) { var str = string.Format("{0}-{1}-{2}-{3}-{4}", f1, f2, f3, f4, f5); Console.WriteLine(str); sb.AppendLine(str); count++; } } } } } } File.WriteAllText(@"C:jbs.txt", sb.ToString()); Console.WriteLine(count); Console.WriteLine(count / Math.Pow(6, 5)); } static void T4() { var shope = new List<String>(); #region 10双鞋 for(int i = 0; i < 10; i++) { byte av=(byte)(65+i); var no=Encoding.ASCII.GetString( new byte[]{av} ); shope.Add(no + "1"); shope.Add(no + "2"); } #endregion var rnd = new Random(Environment.TickCount); var mCount = 0; var total = 10000000; for (int tIndex = 0; tIndex < total; tIndex++) { #region 随机拿8双 var getSet = new List<String>(); while (getSet.Count < 8) { var index = rnd.Next(20); if (getSet.Contains(shope[index])) continue; getSet.Add(shope[index]); } #endregion #region 检测刚好一双的 var olist = getSet.OrderBy(ent => ent).ToList(); var prev = "9"; var pc = 0; for (int i = 0; i < olist.Count; i++) { var cur = olist[i].Substring(0, 1); if (cur == prev) { pc++; } prev = cur; } if (pc == 1) { mCount++; //olist.ForEach(ent => Console.Write(ent + "-")); //Console.WriteLine(); } #endregion } Console.WriteLine("共计:" + mCount +"/" + total); Console.WriteLine("概率:" + (double)mCount / (double)total); } static void T3() { var AT = new string[] { "A", "B", "C", "D", "E" ,"F","G","H"}; int count = 0; var dic = new Dictionary<string, int>(); for (int f1 = 0; f1 < 8; f1++) { for (int f2 = 0; f2 < 7; f2++) { for (int f3 = 0; f3 < 6; f3++) { for (int f4 = 0; f4 < 5; f4++) { for (int f5 = 0; f5 < 4; f5++) { for (int f6 = 0; f6 < 3; f6++) { for (int f7 = 0; f7 < 2; f7++) { for (int f8 = 0; f8 < 1; f8++) { var list = new List<String>(AT); var k1 = list[f1]; list.RemoveAt(f1); var k2 = list[f2]; list.RemoveAt(f2); //ChangeIfGreater(ref k1, ref k2); var k3 = list[f3]; list.RemoveAt(f3); var k4 = list[f4]; list.RemoveAt(f4); ChangeIfGreater(ref k3, ref k4); var k5 = list[f5]; list.RemoveAt(f5); var k6 = list[f6]; list.RemoveAt(f6); ChangeIfGreater(ref k5, ref k6); var k7 = list[f7]; list.RemoveAt(f7); var k8= list[f8]; list.RemoveAt(f8); ChangeIfGreater(ref k7, ref k8); var tmp = string.Format("{0},{1},{2}-{3},{4}-{5},{6}-{7}", k1, k2, k3, k4, k5,k6,k7,k8); if (!dic.ContainsKey(tmp)) { dic[tmp] = 0; Console.WriteLine(tmp); count++; } dic[tmp]++; } } } } } } } } var sb = new StringBuilder(); foreach (var v in dic.Keys) { if(v.IndexOf("C-D")<0)continue; if(v.IndexOf("E-F")<0)continue; if (v.IndexOf("G-H") < 0) continue; sb.AppendLine(v); } File.WriteAllText(@"C:kjbs.txt",sb.ToString()); Console.WriteLine(count); } static void ChangeIfGreater(ref string v1, ref string v2) { if (string.Compare(v1,v2)>0) { var tmp = v1; v1 = v2; v2 = tmp; } } static void T2() { var Arr = new int[] { 1, 1, 2, 2, 3 }; var AT = new string[] { "A", "B", "C", "D", "E" }; var count = 0; var dic = new Dictionary<string, int>(); var rList = new List<String>(); for (int f1 = 0; f1 < 5; f1++) { for (int f2 = 0; f2 < 4; f2++) { for (int f3 = 0; f3 < 3; f3++) { for (int f4 = 0; f4 < 2; f4++) { for (int f5 = 0; f5 < 1; f5++) { var list = new List<int>(Arr); var k1 = list[f1]; list.RemoveAt(f1); var k2 = list[f2]; list.RemoveAt(f2); var k3 = list[f3]; list.RemoveAt(f3); var k4 = list[f4]; list.RemoveAt(f4); var k5 = list[f5]; list.RemoveAt(f5); var tmp = string.Format("{0}-{1}-{2}-{3}-{4}", k1, k2, k3, k4, k5); if (!dic.ContainsKey(tmp)) { dic[tmp] = 1; //Console.WriteLine(tmp); var arr1 = new int[] { k1, k2, k3, k4, k5 }; var g1 = ""; var g2 = ""; var g3 = ""; for (int i = 0; i < 5; i++) { var a = arr1[i]; if (a == 1) { g1 += AT[i] + "-"; } if (a == 2) { g2 += AT[i] + "-"; } if (a == 3) { g3 += AT[i] + "-"; } } if (string.Compare(g1, g2) > 0) { var tmp2 = g1; g1 = g2; g2 = tmp2; } var rStr = g1.Trim("-".ToCharArray()) + " " + g2.Trim("-".ToCharArray()) + " " + g3.Trim("-".ToCharArray()); rList.Add(rStr); Console.WriteLine(rStr + " :" + (count + 1)); count++; } else { dic[tmp]++; } } } } } } //foreach (var item in rList.OrderBy(ent => ent).ToList()) //{ // Console.WriteLine(item); //} Console.WriteLine(count); } static void T5() { var arr = new List<int>(); var person = 6; for (int i = 0; i < person; i++) { arr.Add(0); } Loop(0, person, arr, 0, 12); } static List<String> PersonBirthdayList = new List<string>(); static void Loop(int level,int maxLevel, List<int> arr,int low,int high) { for (int i = low; i < high; i++) { arr[level] = i; if ( (level +1) < maxLevel) { Loop(level + 1, maxLevel, arr, low, high); } else { var line= arr.Aggregate("", (t, e) => t += "-" + e).Trim("-".ToCharArray()); PersonBirthdayList.Add(line); } } } static void T6() { var rnd = new Random(Environment.TickCount); var total = 90000000; var count = 0; for (int tIndex = 0; tIndex < total; tIndex++) { var person = new int[20]; for (int i = 0; i < 20; i++) { person[i] = rnd.Next(1, 13); } var p2 = 0; var p3 = 0; var g = person.GroupBy(ent => ent).ToList(); foreach (var gitem in g) { if (gitem.Count() == 2) { p2++; } if (gitem.Count() == 3) { p3++; } } if (p2 == 4 && p3 == 4) { count++; string line = ""; for (int j = 0; j < person.Length; j++) { line += person[j] + "-"; } // Console.WriteLine(line.Trim("-".ToCharArray())); } } Console.WriteLine(count); Console.WriteLine("共计:" + count + "/" + total); Console.WriteLine("概率:" + (double)count / (double)total); } } }
注意分母如果是20*19*18*17*16*15*14*13是考虑顺序的排列时,那么分子部分也应该是需要考虑顺序的排列 直接按 C(10,1) 18*16*14*12*10*8,只考虑后面6双鞋的排序,
所以要把前面凑成一双的2只鞋补上去, 补第一只时,我们有7个位置可选,然后补第二只我们有8个位置可选,所以要乘以7*8=56
如果分母是 C(20,8) 是组合 那么需要“消除” 18*16*14*12*10*8 这6只鞋的排列数,也就是除以 6!