遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

同时丢5个色子同时出现2对一样概率

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            T2();
            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 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);
        }
    }
}
View Code

按 C(5,2) *6  C(3,2)*5 *4  /   6^5算

5个里取2个,这一组由6种点数

余下的3个里取2个,这组里有5种点数

余下的1个有4种点数

但是结果是3600,实际是1800,注意上面组合输出可见,A-B  C-D E   与 C-D  A-B E是重复的,

因为将5个色子,分成  2  2  1三组,组内部考虑重复,但是组间是有顺序的,由于 一二两组都是 2 个元素,所以一二组是重复了2!次

 

posted on 2019-03-15 10:04  遗忘海岸  阅读(2790)  评论(0编辑  收藏  举报